1

I am new to assembly programming and I am trying to compile/assemble the code bellow that I found on a book. I am using nasm, but I keep getting this error:

error: symbol 'a1' undefined

I am not sure how to solve this. I have tried to compile this on my ubuntu(64 bit) host machine and also on a Freebsd 32bit virtual machine and both times I got the same error.

global _start
_start:
xor eax,eax


jmp short string
code:
pop esi
push byte 15
push esi
push byte 1
mov a1,4
push eax
int 0x80


xor eax,eax
push eax
push eax
mov a1,1
int 0x80


string:
call code
db 'Hello world !', 0x0a
Yuran Pereira
  • 258
  • 3
  • 11
  • 2
    AL (as in the letter "L", not the digit "1"). It means "Lower byte of register A". Similarly, "ah" is the upper byte, and "ax" both bytes of of 16-bit register "A". – FoggyDay Dec 27 '19 at 06:25
  • 1
    Also, `push byte 1` is mostly meaningless. Byte operand-size is impossible for `push`, only 16 or 32-bit. `push strict byte 1` would specify how the immediate is encoded, but NASM will already encode it as `push imm8` because the number is small enough to fit. – Peter Cordes Dec 27 '19 at 06:30

1 Answers1

2

That should be mov al, 4. It's a lower case L, not the number 1, so that you have the al register. Ditto for mov al, 1 further down.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • Thank you so much, that worked and compiled without errors. But I got one more issue when trying to run on my FreeBSD it says "Exec format error. Binary file not executable." even though I followed all the instructions and used "ld -s -o hello hello.o" to link. Do you know why that might be happening? – Yuran Pereira Dec 27 '19 at 06:34
  • 1
    @YuranPereira: No, I don't. Ask that as a separate question, please. – Nate Eldredge Dec 27 '19 at 06:34
  • alright, thank you for your help :) – Yuran Pereira Dec 27 '19 at 06:37
  • 1
    @YuranPereira: FreeBSD and Linux use different executable metadata around similar machine code. Probably you used Linux's linker to make a Linux executable and tried to run it on FreeBSD. FreeBSD also uses a different ABI / calling convention for system calls than Linux, especially for 32-bit executables. Making system-calls directly is OS-specific; don't assume that asm source code is portable to any other OS. For example, the code in your question will only work on 32-bit x86 FreeBSD - it passes system call args on the stack instead of registers. Do all your assembling + linking on BSD. – Peter Cordes Dec 27 '19 at 06:48
  • @PeterCordes thank you for that explanation. The binary that I am trying to run on BSD has been assembled and linked on the same system, but for some reason I keep getting the "Exec format error" – Yuran Pereira Dec 27 '19 at 07:25
  • 1
    @YuranPereira: the I agree with Nate; you need to ask a separate question. Make sure to include the exact commands you used, and that you definitely ran everything in your FreeBSD VM. Also include the output of `file ./my_program` and also `ld --version` – Peter Cordes Dec 27 '19 at 09:03