After initializing a word inside the .data segment like this:
.data
base: .word 0
I need to change the address that 'base' is saved into, inside the .text segment. For example if 'base' is stored in address '268501692', I need to change it into '268501700'
How can I do this?
From your comment:
I basically want to allocate memory through syscall 9, and write the address of v0 (which is the base of the allocated address space) into a .data segment tag, and also being able to manipulate it afterwards.
In C terms, what you're asking is to modify the address of static int array[]
to set it to the return value of malloc
.
That's not how symbols work. When you assemble and link, they become fixed numeric addresses. There's no symbol-table lookup happening when lw $t0, base($zero)
runs: the the machine-code instruction has the address hard-coded as an immediate constant. See a MIPS ISA reference for the encoding format.
What you should do instead is store that pointer in a register, or in a fixed memory location, i.e. static int *pointer
. Then you only have to modify the value of the pointer, it still has its own address. This is an extra level of indirection over a static array, but only if you keep the pointer in memory. With a pointer in a register, it doesn't matter whether it's pointing to a static array or to dynamically allocated memory.
Don't let C syntax fool you: pointer[10]
looks the same as array[10]
, but in the pointer case (with a static
or global pointer
variable), the compiler has to emit asm that first loads the pointer from memory, then dereferences it. But in the array case, the address of array
is a link-time constant, so the compiler can access array[10]
directly because that's also a link-time constant.