1

I've been trying to learn assembly. So for practice I'm trying to code snake, and I've run into an issue.

Whenever I try to load the value of direction_offset into a register it just becomes absurdly large. I'm expecting it to become 3 but instead if becomes 251785475. Could anyone explain to me why this happens?

Snippet from .data section:

direction_offset db 3
direction db -15,1,15,-1
        ; grab offset to new position from array
        mov esi, [direction_offset]
        mov edi, [direction + esi]

gdp output:

198     mov esi, [direction_offset]
(gdb) info register esi
esi            0xe1                225
(gdb) step
199     mov edi, [direction + esi]
(gdb) info register esi
esi            0xf01f103           251785475
(gdb)

In case it matters, I compiled the code using nasm like this:

#compile
nasm -g -f elf -F dwarf $asmname
ld -m elf_i386 -o $name $outputname
Peter Cordes
  • 286,368
  • 41
  • 520
  • 731
Snappie
  • 111
  • 2
  • 2
    You loaded 4 bytes from your `db` byte array into a dword register, so yeah; look at the register value in hex and you'll see the value you wanted in the low byte. Use a `movzx` load. (But kudos for at least using a debugger to narrow down the problem to just this. We get a lot of variations of this question where people loaded a dword that overlaps multiple bytes, so if someone wants to write up a canonical answer, this is probably the nicest question, nice [mcve] and not a lot of noise.) – Peter Cordes Sep 30 '21 at 02:21
  • @PeterCordes Def going to try that tomorrow when I pick it back up. thanks! – Snappie Sep 30 '21 at 02:33

0 Answers0