Loading a byte from a DB 1, but printf integer show a large number

db declares 8-bit (one byte) values, while %d prints 32-bit (four byte) values on x86.

In effect, when loading 32-bit register eax with mov eax, [an] you are loading bits of letters "num" to high bytes of the register. They are later printed as number, when using %d or ignored when using %c.

To declare 32 bit values you should use dd, instead of db.


@zch pointed out the issue. But if you really do want to print out a byte data item as an integer and don't have the luxury of redefining it, you can do it this way:

     movsx eax, BYTE [an]       ; [an] is a byte value to be printed with %d
     push  eax
     push  dword format
     call  printf

The movsx instruction sign extends an 8-bit or 16-bit operand (in this case, the 8-bit operand, [an]) into the 32-bit register, eax. If it is unsigned, then you'd use movzx eax, [an] (zero fill). Normally in C, the promotion to integer is done implicitly. But in assembly, you need to do that yourself.