MIPS Assembly - lui $ t0, 4097?

Can someone explain to me how lui works, which means " 4097 ", which means adding 8 to $t0 ?

 .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 
+5
source share
3 answers

4097 = 1001 hex

therefore, the first instruction puts 0x10010000 in the t0 register. lui is “upper upper load”, and “upper” means the upper 16 bits, and “immediate” means that you give it a literal value (4097). 4097 as the "upper" value becomes 0x10010000.

ori is "or immediate", with 8 being an immediate value, so the resulting address in a0 is 0x10010008, which is the address that Aarray lives in.

The last lw command is the “boot word”, which is loaded from the memory address into t0 (which is still 0x10010000 at the moment) plus 4 bytes (4 is an offset from t0 and leads to the address where ALength resides) 4 bytes of data in a1.

+15
source

lui $ t0, 4097 - upper online load instruction. The immediate value (4097) is shifted 16 bits to the left and stored in the register $ t0. The bottom 16 bits are zeros.

+7
source

This "does it manually" to generate static addresses, as a workaround for the MARS assembler, which lacks %hi(symbol) and %lo(symbol) for the linker to fill 4097 ( 0x1001 ) from the upper half of Alength and Aarray , as well as 4 and 8 of the bottom half of these addresses.

This code assumes that MARS will put the .data section at 0x10010000 , as the line with the .data directive says.

Two lines add up to 4 bytes, so the word values ​​are already word aligned. This .align code directive .align , even if it ended with zero bytes (until you add another line and your word is not interrupted).


If you compile this C on the Godbolt compiler explorer :

 int x = 1; // with no initializer it goes in the .bss as .space 4 int foo() { return x; } 

MIPS gcc5.4 -O3 gives you this assm (some noise is removed):

 foo(): lui $2,%hi(x) lw $2,%lo(x)($2) j $31 nop # IDK why it didn't put the load in the branch-delay slot; clang does .data .p2align 2 # actually gcc uses .align 2 but I disambiguated x: .word 1 

$2 is $v0 , the return value in the standard MIPS calling convention.

0
source

Source: https://habr.com/ru/post/1384630/


All Articles