While learning assembly language from a book there is a listing showing some basic operations:
segment .data
a dq 176
b dq 4097
segment .text
global _start
_start:
mov rax, [a] ; Move a into rax.
add rax, [b] ; add b o rax.
xor rax, rax
ret
After assembling with "$yasm -f elf64 -g dwarf2 -l listing.lst listing.asm"
command and linking with "$ld -o listing listing.o"
I ran the program in gdb. There whenever I tried to print the value of a variable, gdb showed this error message:
(gdb) p a
'a' has unknown type; cast it to its declared type
Same for the other variable 'b'. However casting 'a' or 'b' for int worked:
(gdb) p (int)a
$11 = 176
(gdb) p (int)b
$12 = 4097
But isn't this supposed to work without casting? Why do I need to cast? What mistake I've made in my source file?
dq
is most likely NOTint
on your target platform (I'm never sure from head on which target platform theint
is 64b). So you are probably printing only 32 bit part of the value by the casting. (and I'm also not sure what type the gdb recognizes, doesp (int64_t)a
work, or you have to use base C types likep (long long)a
? Or even some asm are recognized likep (qword)a
?) – Compassmov rax,[a]
does read 8 bytes, so it should be thenmov eax,[a]
if you want to use onlydd
to define the data (ormovsx rax,dword [a]
in case you want sign-extend the 32 bit memory value into 64 bitrax
). Also I don't see howdq
vsdd
would fix anything, it just changes amount of memory defined after labela
. Giving labela
some kind of type information in debug info is beyond what I usually do in assembly (you make me sort of recognize how the 8 bit era forced me so much to keep track of everything in head, that I didn't even think about debugger being aware of "type"). – Compassa
may get actually into your way (if you access that memory by different instructions, for example if you vectorize some code, accessing several elements at one time). – Compassint32_t a = 1;
does define 32 bit integer, but in assembly something likea: dd 1
does define label pointing at next first byte of emitted machine code (thea:
part of line), and then it defines four bytes1, 0, 0, 0
(thedd 1
part of line), they are like disconnected entities, you can even use two lines. – Compass