Search code examples
mipsmips32instruction-setspimqtspim

How to set the memory adress for an instruction in MIPS?


Normally, the SPIM simulator itself allocates an address to the instructions in a program. Is there some way to manually choose where to store a particular instruction?


Solution

  • Sure, the .text directive has an optional argument where you can specify an address:

    .text
    .globl main
    # This code ends up at the default address for the .text section
    main:
    
    li $a0,1
    jal foo
    li $v0,1
    syscall
    
    li $v0,10
    syscall
    
    .text 0x430000
    # This code ends up at address 0x430000
    foo:
    li $a0,2
    jr $ra
    

    Of course you can't just pick an address at random. It must be valid for the target environment (QtSpim in my example).


    Another possibility is to assemble everything into the default location, and then copy parts of the code into RAM at runtime and execute it from there.