Can someone explain me how does lui
works, what does "4097
" stand for, what does adding 8
to $t0
mean?
.data 0x10010000
blank: .asciiz " " # 4097
newline: .asciiz "\n" # 4097 + 2
#input_start
Alength: .word 13
Aarray: .word 130, 202, 30, 4440, 530, 532, 33, 204, 8, 524, 8933, 92, 10
#input_end
.text
lui $t0, 4097
ori $a0, $t0, 8 # address of A[]
lw $a1, 4($t0) # load length
LUI -- Load upper immediate The immediate value is shifted left 16 bits and stored in the register. The lower 16 bits are zeroes.
— The load upper immediate instruction lui loads the highest 16 bits of a register with a constant, and clears the lowest 16 bits to 0s. This illustrates the principle of making the common case fast. — Most of the time, 16-bit constants are enough.
lui. load upper immediate. Build 32-bit constants and uses the U-type format. LUI places the U-immediate value in the top 20 bits of the destination register rd, filling in the lowest 12 bits with zeros.
The lui (load upper immediate) and ori (or immediate) instructions are used to load a 32 bit address in two 16 bit pieces: lui R, hi_bits ori R, lo_bits. The assembler divides the address of Label into two 16 bit half-words, consisting of the most and least significant halves, called hi_bits and lo_bits.
4097 = 1001 hex
so, the first instruction puts 0x10010000 into register t0. lui is "load upper immediate", with "upper" meaning the upper 16 bits, and "immediate" meaning that you are giving it a literal value (4097). 4097 as an "upper" value becomes 0x10010000.
ori is "or immediate", with 8 being the immediate value, so the resulting address in a0 is 0x10010008, which is the address where Aarray lives.
The final instruction lw is "load word" which loads from the memory address in t0 (which at this point is still just 0x10010000) plus 4 bytes (the 4 is an offset from t0 and results in an address where ALength lives) 4 bytes of data into a1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With