2

I code simple assembly hello_word.asm below:

global _start
section .text
_start:
       mov rax, 1
       mov rdi, 1
       mov rsi, hello_word
       mov rdx, len_hw
       syscall

       mov rax, 60
       mov rdi, 1
       syscall
section .data
       hello_word: db  'Hello word ! I am ctnguyenvn', 0xa
       len_hw:     equ $-hello_word

after i compile using nasm and ld (x64 bit)

nasm -f elf64 hello_word.asm -o hello_word.o
ld hello_word.o -o hello_word

when i using gdb to check

 (gdb) set disassembly-flavor intel 
 (gdb) break _start 
  Breakpoint 1 at 0x4000b0
 (gdb) run
  Starting program: /data/MEGA/Self_Learning/ASM/HelloWord 

  Breakpoint 1, 0x00000000004000b0 in _start ()
  (gdb) disassemble 
  Dump of assembler code for function _start:
  => 0x00000000004000b0 <+0>:   mov    eax,0x1
     0x00000000004000b5 <+5>:   mov    edi,0x1
     0x00000000004000ba <+10>:  movabs rsi,0x6000d8
     0x00000000004000c4 <+20>:  mov    edx,0x1d
     0x00000000004000c9 <+25>:  syscall 
     0x00000000004000cb <+27>:  mov    eax,0x3c
     0x00000000004000d0 <+32>:  mov    edi,0x1
     0x00000000004000d5 <+37>:  syscall 
  End of assembler dump.
  (gdb) 

My question: why when i code using rax (64bit) but then when using gdb i get the result is eax (32bit).

  => 0x00000000004000b0 <+0>:   mov    eax,0x1
     0x00000000004000b5 <+5>:   mov    edi,0x1
     ...

How can I not change that result? (did i have compile wrong ?)

My info:

  • nasm: version 2.13.03

  • gdb : 8.1

  • Linux: 4.16.8

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
ctnguyenvn
  • 33
  • 7
  • 4
    Your friendly assembler optimized that for you. 32 bit operations automatically zero the top 32 bits so `mov eax, 1` is the same as `mov rax, 1` except it's shorter machine code. You can force 64 bit as follows: `mov rax, strict qword 1`. See also [strict in the nasm manual](https://www.nasm.us/xdoc/2.11.08/html/nasmdoc3.html#section-3.7). – Jester Sep 13 '18 at 13:15
  • @Jester That's actually a perfect valid and correct answer, not a comment :) – m0skit0 Sep 13 '18 at 13:19
  • It's probably a duplicate though just I couldn't find one yet. – Jester Sep 13 '18 at 13:28
  • 1
    @Jester: found it with a google search for `site:stackoverflow.com nasm "mov rax, strict qword"` – Peter Cordes Sep 13 '18 at 17:38

0 Answers0