1

Im trying to do a loop in NASM that simply writes to a variable 1 and then do it again but 1 byte up on the variable... i have come up with this design

    mov ebx, 0
    mov rax, 1
    test_loop:
    cmp ebx, 4
    je test_loop_end
    mov [numbet_to_text + ebx], al
    inc ebx
    jmp test_loop
    test_loop_end:

but when i check it with this, it doesn't call:

    mov ebx, [numbet_to_text]
    cmp ebx, 1111
    je teste

Checked with gdb and saw that when i wrote to a byte after writing one of them it got corrupted so...
write to byte 0 --->1 ok, but when I write to byte up I would get byte 0----> random value.. and byte 1 ----> 1

some GDB screenshots to explain myself better: print1 print2

as you see on the print when i write to 0x40201b i got a 1... thats ok... but when i write a byte up i would get 0x40201c 1... ok but 0x40201b would get a random value in this case 257 and i cant see why

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Rabyt
  • 61
  • 6

1 Answers1

3
mov ebx, [numbet_to_text]
cmp ebx, 1111
je teste

After having writen 4 consecutive bytes in the memory at numbet_to_text, the value that you can expect would be 0x01010101. Certainly not the decimal number 1111.

0x40201B contains byte 0x01
0x40201C contains byte 0x01
0x40201D contains byte 0x01
0x40201E contains byte 0x01

After the 1st write 0x40201B shows 0x00000001 (1)
After the 2nd write 0x40201B shows 0x00000101 (257)
After the 3rd write 0x40201B shows 0x00010101 (65793)
After the 4th write 0x40201B shows 0x01010101


mov rax, 1
mov [numbet_to_text + ebx], al

If there's any chance that this is 64-bit code then better write:

mov [numbet_to_text + rbx], al
Sep Roland
  • 33,889
  • 7
  • 43
  • 76