Recieving 32-bit registers from 64-bit nasm code

themustang

I am learning 64-bit nasm, I assemble the .nasm file, which ONLY contains 64-bit registers, by doing the following

nasm -f elf64 HelloWorld.nasm -o HelloWorld.o

and link it doing the following

ld HelloWorld.o -o HelloWorld

the program runs correctly and even says it is a 64-bit ELF when I run the file command, but when I use objdump or gdb to disassemble the executable, the registers I put as 64-bit registers in the code show up as 32-bit registers when disassembled. (example: rax in source showing up as eax when disassembled)

Why is this?

This does not happen on just one computer, and it is a new problem, it hasn't been doing this before.

HelloWorld.nasm:

global _start

section .text

_start:
        mov rax, 1
        mov rdi, 1
        mov rsi, hello_world
        mov rdx, length
        syscall

        mov rax, 60
        mov rdi, 11
        syscall

section .data

        hello_world: db 'Hello World',0xa
        length: equ $-hello_world

Disassembled HelloWorld:

...
00000000004000b0 <_start>:
  4000b0:       b8 01 00 00 00          mov    eax,0x1
  4000b5:       bf 01 00 00 00          mov    edi,0x1
  4000ba:       48 be d8 00 60 00 00    movabs rsi,0x6000d8
  4000c1:       00 00 00
  4000c4:       ba 0c 00 00 00          mov    edx,0xc
  4000c9:       0f 05                   syscall
  4000cb:       b8 3c 00 00 00          mov    eax,0x3c
  4000d0:       bf 0b 00 00 00          mov    edi,0xb
  4000d5:       0f 05                   syscall
...
Andreas Fester

Why does

...
mov rax, 1
mov rdi, 1
mov rsi, hello_world
...

gets disassembled as

...
4000b0:       b8 01 00 00 00          mov    eax,0x1
4000b5:       bf 01 00 00 00          mov    edi,0x1
4000ba:       48 be d8 00 60 00 00    movabs rsi,0x6000d8
4000c1:       00 00 00
...

Because the literal 0x1 fits into 32 bits, and the upper 32 bits of a 64 bit register are set to 0 when loading the lower 32 bits through the corresponding E-register. Hence the assembler can optimize the mov to a 32 bit operation.

Note that the address loaded into rsi might not fit into 32 bits, hence rsi remains as such.

If you add the following instructions, you can see the effect very clearly:

mov rbx, 0x0ffffffff      ; still fits into 32 bit
mov rbx, 0x100000000      ; does not fit into 32 bits anymore

gets disassembled as

 a: bb ff ff ff ff          mov    $0xffffffff,%ebx
 f: 48 bb 00 00 00 00 01    movabs $0x100000000,%rbx
16: 00 00 00 

You can disable nasm optimization with -O0, in which case the instructions keep their long format:

nasm -O0 -f elf64 HelloWorld.asm 

Result:

14: 48 bb ff ff ff ff 00    movabs $0xffffffff,%rbx
1b: 00 00 00 
1e: 48 bb 00 00 00 00 01    movabs $0x100000000,%rbx
25: 00 00 00 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recieving 32-bit registers from 64-bit nasm code

From Dev

How to link 32-bit Nasm assembly object code on a 64-bit windows computer

From Dev

How to link 32-bit Nasm assembly object code on a 64-bit windows computer

From Dev

Assembly: 64 bit multiplication with 32-bit registers

From Dev

Wix: How to write to 32bit registers on 64 bit machine

From Dev

SSE 64 bit registers

From Dev

Migrating from 64 bit to 32 bit

From Dev

moving from ubuntu 32 bit to 64 bit

From Dev

going from 32 bit to 64 bit in assembly

From Dev

moving from ubuntu 32 bit to 64 bit

From Dev

32 bit registers act as 8 bit ones

From Dev

How to detect the JRE bit(32 or 64-bit) from VC++ code

From Dev

32 bit vs 64 bit

From Dev

Assembly: division using the values in two 32-bit registers as if they were one 64-bit integer

From Dev

Assembly: division using the values in two 32-bit registers as if they were one 64-bit integer

From Dev

is it possible to run 64 bit code in a machine with 32 bit processor?

From Dev

Why does this code work on iOS 32 bit but not on 64 bit?

From Dev

C code 32 to 64 bit type conversion

From Dev

Mach-O 64-bit format does not support 32-bit absolute addresses. NASM

From Dev

Mach-O 64-bit format does not support 32-bit absolute addresses. NASM

From Dev

Issue with Linking a 32-bit NASM file on a 64-bit machine

From Dev

multiplying two 32-Bit Numbers and printing the 64 bit result as decimal NASM assembly

From Dev

Calling VirtualQueryEx function from a 64 bit process on a 32 bit one

From Dev

extract 32bit from 64bit integer

From Dev

Hooking a 64 bit process from 32 bit app

From Dev

Switch from Ubuntu 13.04 64bit to 32bit

From Dev

Using 32 bit library from 64 bit project - .NET

From Dev

Send a packet from 32 bit and Receive in 64 bit machine

From Dev

Unsigned int from 32 bit to 64bit OS

Related Related

  1. 1

    Recieving 32-bit registers from 64-bit nasm code

  2. 2

    How to link 32-bit Nasm assembly object code on a 64-bit windows computer

  3. 3

    How to link 32-bit Nasm assembly object code on a 64-bit windows computer

  4. 4

    Assembly: 64 bit multiplication with 32-bit registers

  5. 5

    Wix: How to write to 32bit registers on 64 bit machine

  6. 6

    SSE 64 bit registers

  7. 7

    Migrating from 64 bit to 32 bit

  8. 8

    moving from ubuntu 32 bit to 64 bit

  9. 9

    going from 32 bit to 64 bit in assembly

  10. 10

    moving from ubuntu 32 bit to 64 bit

  11. 11

    32 bit registers act as 8 bit ones

  12. 12

    How to detect the JRE bit(32 or 64-bit) from VC++ code

  13. 13

    32 bit vs 64 bit

  14. 14

    Assembly: division using the values in two 32-bit registers as if they were one 64-bit integer

  15. 15

    Assembly: division using the values in two 32-bit registers as if they were one 64-bit integer

  16. 16

    is it possible to run 64 bit code in a machine with 32 bit processor?

  17. 17

    Why does this code work on iOS 32 bit but not on 64 bit?

  18. 18

    C code 32 to 64 bit type conversion

  19. 19

    Mach-O 64-bit format does not support 32-bit absolute addresses. NASM

  20. 20

    Mach-O 64-bit format does not support 32-bit absolute addresses. NASM

  21. 21

    Issue with Linking a 32-bit NASM file on a 64-bit machine

  22. 22

    multiplying two 32-Bit Numbers and printing the 64 bit result as decimal NASM assembly

  23. 23

    Calling VirtualQueryEx function from a 64 bit process on a 32 bit one

  24. 24

    extract 32bit from 64bit integer

  25. 25

    Hooking a 64 bit process from 32 bit app

  26. 26

    Switch from Ubuntu 13.04 64bit to 32bit

  27. 27

    Using 32 bit library from 64 bit project - .NET

  28. 28

    Send a packet from 32 bit and Receive in 64 bit machine

  29. 29

    Unsigned int from 32 bit to 64bit OS

HotTag

Archive