当前位置:网站首页>Risc-v assembly language programming (2) assembly program ASM_ run_ led

Risc-v assembly language programming (2) assembly program ASM_ run_ led

2022-06-24 05:50:00 IC knowledge base

Use RISC-V assembly language , Write a led The experiment of running lantern

Program analysis :

LI x8, 0xf0000000; # Set up gpio address;

LI It's a pseudo instruction , Can be translated into by compiler LUI Instructions , This instruction sets the value to 0xf000_0000 To register x8 in , The purpose is to gpio Store your address in x8 Register for later use .

ADDI x6,x0,0 ; # initialization Variable x6 =0;

x0 The value of and 0 Add and store in x6 In the register . It means will x6 Register clear .risc-v There are no special instructions in the assembly instruction set to clear .

LI x7, 0x00400000; # x7 Set up delay counter

Will value 0x0040_0000 Store in x7 In the register .x7 Register as delay counter Timer usage . Make use of the time delay generated by the program itself LED Time delay 1s. Probably run 40_0000 This cycle requires 1s.

START: ADDI x10, x0, 0x80; # x10 = 0x80, set gpio bit 7

START Is the address label , Follow me “:”,ADDI It's the instruction code , Between registers and registers or immediate numbers “,” separate . The command ends with “;”. If you want to add notes, you should use “#”. The compiler does not compile “#” The following sentence .

The order x0 The value of and 0x80 Add up , Save and to x10 in . because x0 The value of is 0, In fact, the effect makes the value 0x80 Deposit in x10 In the register .X10 Registers are used to turn on and off LED.0x80 Corresponding 2 Hexadecimal number is 1000_0000. But at present, we can not x10 The values in are output directly to gpio On the address of . because led It's Gongyang design , One end is already 3.3V High level ,led7 Corresponding GPIO If the interface is 1,led7 Will not be lit . But others led The light will be lit , because led6-0 At this time, it is low level . So we need to x10 The value of is negated as a whole .

NOT x18, x10; # x18 = ~x10 Reverse output

NOT Instructions are pseudo instructions , The order x10 The value stored in memory is reversed and stored in x18 in . The last output value exists x18 Register .

SH x18, 0(x8); # addr[0xf000_0000] = val[0x18]

hold x18 The value of is stored in an address , Address by x8 And the immediate value of 0 Add up to get . because X8 The previously saved value in is gpio The address of , So here is the x18 The value of is output to gpio On .

SH x0, 4(x8); # addr[0xf000_0004] = 0 as output

use gpio My address plus 4, Generate direction control register address f000_0004. Use store Give orders to x0 The value of is stored in the address f000_0004 Up . In this way, the value of the direction control register is set to 0, Lighten up led7.

When the lamp is on, it needs to be kept 1s, Perform a delayed operation . Through the program loop Loop to achieve .

ADDI x6, x6, 1; # x6 = x6 + 1

x6 As an accumulator , Add... Once per cycle 1

BNE x6, x7, LOOP; # if(x6 != x7) goto loop

Judge When x6 It's not equal to x7 when , The program jumps to loop. That is to say x6 Continue to add up , until x6 be equal to x7,loop Cycle is completed . Proceed to the next instruction .

ADDI x6, x0, 0; # x6 = 0

Zero clearing x6, because x6 This accumulator then lights up other lights and delays 1s It will also be used .

SRLI x10, x10,1; # x10 >> 1 , Move right 1 bit

use srli Instructions , The order x10 Shift the value in to the right 1 position , Turn on the next light .

BEQ x10, x0, START; # if(x10 == 0x0) goto start

After moving x10 The value of is determined by 0x80 (1000_0000) become 0x40 (0100_0000), And then move to the right to become 0x20, Finally become 0. At this time, all after the inversion LED Are extinguished . In order to cope with all 0 The situation requires BEQ sentence . When x10 Is full of 0 Jump to start That is where the program starts .X10 The value of is assigned to 0x80.

NOT x18, x10; # x18 = ~x10

SH x18, 0(x8); # addr[0xf000_0000] = x18

JAL LOOP; # pc = loop

If x10 It's not equal to 0, prove x10 The binary representation contains 1. Not equal to 0, here x10 Be reversed and output , It's like lighting up the next light . And then jal Jump to loop Lilai delay 1s.

remarks : Please search the search engine for the complete content “IC The knowledge base ” see .

原网站

版权声明
本文为[IC knowledge base]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210803140109205V.html

猜你喜欢