Search code examples
assemblyx86nasmcpu-registers

NASM error: invalid operands in non-64-bit mode


I am new to assembly programming. I wrote a small program to add two numbers.

; A test program

%include    'libasm/system.asm'
%include    'libasm/numbers.asm'
%include    'libasm/string.asm'

SECTION     .text
global      _start
_start:
    mov     r8d,    12    ;First number
    mov     r9d,    15    ;Second number
    add     r8d,    r9d   ;Adding two numbers and storing result in r8d
    mov     eax,    r8d   ;Moving the result in eax so that it can be printed
                          ;with iprintLF 
    call    iprintLF      ;Displaying the result
    call    quit          ;Exiting the program

My question is why does this program fails to compile with the following error:

nasm -f elf test.asm
test.asm:10: error: invalid operands in non-64-bit mode
test.asm:11: error: invalid operands in non-64-bit mode
test.asm:12: error: invalid operands in non-64-bit mode
test.asm:13: error: invalid operands in non-64-bit mode

As far as I understand r8d, r9d and eax are all 32 bit registers and not preserved.
https://www.cs.uaf.edu/2017/fall/cs301/reference/x86_64.html
Subroutine iprintLF (written in libasm/numbers.asm and works fine) is used to print integer stored in eax. I am using Ubuntu in WSL

Distributor ID: Ubuntu
Description:    Ubuntu 20.04.2 LTS
Release:        20.04
Codename:       focal

and my nasm version is

NASM version 2.14.02

Solution

  • As far as I understand r8d, r9d and eax are all 32 bit registers and not preserved.

    Yes, but you can't use the r?d registers while not in 64-bit mode. Because in 32-bit mode, there are only 8 general-purpose registers: eax, ebx, ecx, edx, edi, esi, ebp, esp. x86-64 specifically adds 8 new 64-bit registers r8-r15, whose lower 32-bit halves are r8d-r15d