Assembly Language for Beginners

(nextflipdebug2) #1

1.24 Structures


38: 000420c0 sll a0,a0,0x3
3c: 00821021 addu v0,a0,v0
40: 00022080 sll a0,v0,0x2
44: 00441021 addu v0,v0,a0
48: 3c043c6e lui a0,0x3c6e
4c: 3484f35f ori a0,a0,0xf35f
50: 00441021 addu v0,v0,a0
54: ac620000 sw v0,0(v1)
58: 03e00008 jr ra
5c: 30427fff andi v0,v0,0x7fff

objdump -r rand_O3.o


RELOCATION RECORDS FOR [.text]:
OFFSET TYPE VALUE
00000000 R_MIPS_HI16 .bss
00000008 R_MIPS_LO16 .bss
0000000c R_MIPS_HI16 .bss
00000010 R_MIPS_LO16 .bss
00000054 R_MIPS_LO16 .bss


Let’s consider the two relocations for themy_srand()function.


The first one, for address 0 has a type ofR_MIPS_HI16and the second one for address 8 has a type of
R_MIPS_LO16.


That implies that address of the beginning of the .bss segment is to be written into the instructions at
address of 0 (high part of address) and 8 (low part of address).


Therand_statevariable is at the very start of the .bss segment.


So we see zeros in the operands of instructionsLUIandSW, because nothing is there yet— the compiler
don’t know what to write there.


The linker will fix this, and the high part of the address will be written into the operand ofLUIand the low
part of the address—to the operand ofSW.


SWwill sum up the low part of the address and what is in register $V0 (the high part is there).


It’s the same story with the my_rand() function: R_MIPS_HI16 relocation instructs the linker to write the
high part of the .bss segment address into instructionLUI.


So the high part of the rand_state variable address is residing in register $V1.


TheLWinstruction at address 0x10 sums up the high and low parts and loads the value of the rand_state
variable into $V0.


TheSWinstruction at address 0x54 do the summing again and then stores the new value to the rand_state
global variable.


IDA processes relocations while loading, thus hiding these details, but we should keep them in mind.


1.23.5 Thread-safe version of the example.


The thread-safe version of the example is to be demonstrated later:6.2.1 on page 742.


1.24 Structures


A C/C++ structure, with some assumptions, is just a set of variables, always stored in memory together,
not necessary of the same type^157.


(^157) AKA“heterogeneous container”

Free download pdf