Defining “variables” in assembly language

六眼飞鱼酱① 提交于 2019-12-04 04:42:06

问题


I underdstand that this is extremely stupid quiestion, but I can't figure an answer for some time
How do I correctly declare and define "variables" in GAS AT&T assembly language?
For example, I want buffer for 5 bytes, two 1-byte variables (initially with 0 value), 2-byte variable with 0 and 2-byte variable with 10.
This code doesn't work correctly, at least debugger says (on the first line of the program, after these declarations, just nop instruction) that b and c are big numbers instead of zeros.

.bss
    .lcomm r, 5

.data
    a:  .byte 0
    b:  .byte 0
    c:  .word 0
    d:  .word 10

回答1:


Here's what you see in your "Watches" window:

a = 0 = 0x00 = 0x0000 = 0x00 0000 = 0x0000 0000

b = 167772160 = 16777216 * 10 = 0x1000000 * 0x0A = 0xA000000

c = 655360 = 65536 * 10 = 0x10000 * 0x0A = 0xA0000

d = 10 = 0x0A = 0x0000 000A

What does it mean? It means that your compiler did its job, but your debugger reads c and b as doublewords (4 bytes) instead of bytes.

When it reads in b, it reads its value 0x00, c´s value 0x0000, and d´s value 0x0A on the top, together making it 0xA000000.

Similar thing happens to c. a got lucky, as the next 4 bytes are zero, so the a is really zero.

However, this doesn't always have to be the case. Nothing says that there can't be any garbage after d, not to mention that variables equal to zero may appear in .bss (on a completely different memory location).



来源:https://stackoverflow.com/questions/30559082/defining-variables-in-assembly-language

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