Search code examples
assemblygnu-assembleratt

Defining "variables" in assembly language


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

Solution

  • 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).