MIPS assembly: how to declare integer values in the .data section?

旧街凉风 提交于 2021-01-17 08:20:05

问题


I'm trying to get my feet wet with MIPS assembly language using the MARS simulator.

My main problem now is how do I initialize a set of memory locations so that I can access them later via assembly language instructions?

For example, I want to initialize addresses 0x1001000 - 0x10001003 with the values 0x99, 0x87, 0x23, 0x45. I think this can be done in the data declaration (.data) section of my assembly program but I'm not sure of the syntax. Is this possible?

Alternatively, in the .data section, how do I specify storing the integer values in some memory location (I don't care where, but I just want to reference them somewhere). So I'm looking for the C equivalent of "int x = 20, y=30, z=90;" I know how to do that using MIPS instructions but is it possible to declare something like that in the .data section of a MIPS assembly program?


回答1:


You don't usually initialize specific memory locations; each section (including .data) is positioned at link time, and relocations are resolved then

To make a relocation on a data entry, you choose a name and put name: before it, so you can refer to it by name later. You specify a data block using .size value. For example:

.data
    x: .word 20
    y: .word 30
    z: .word 90

Then you can use the labels in your assembly:

.text
    lw $t0, x



回答2:


you can see more here: MIPS Assembly Overview and here: MIPS dataseg




回答3:


so if I declared x: .word 701 y: .word 701 then .text bge y, x, endin the main body of the program, the condition would accept the integer variables x and y thus allowing the end method to be completed?

I believe this is wrong, specifiying x or y inside your mips program will only return you the base address of x and y. For example, if you typed

addi $t0,y,8

Would give you $t0 = 10000010 (assuming address of y begins at 10000000)

The correct way to compare 2 values from 2 word with labels x and y would be something like

.data
x: .word 701
y: .word 701

.text
main:
   lw $t0,x              #loads $t0 with 701
   lw $t1,y              #loads $t1 with 701
   bge $t0,$t1,end       #compares $t0 and $t1, if equal, jump to address [end]

end:
   #the code segment for end label


来源:https://stackoverflow.com/questions/2619711/mips-assembly-how-to-declare-integer-values-in-the-data-section

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!