Other assembly programming with nasm

Hello,

how to compile a simple program (written in assembly) with nasm?

Code:
SECTION .data
msg     db      'Hello World!', 0Ah
 
SECTION .text
global  _start
 
_start:
 
    mov     edx, 13
    mov     ecx, msg
    mov     ebx, 1
    mov     eax, 4
    int     80h
 
    mov     ebx, 0      ; return 0 status on exit - 'No Errors'
    mov     eax, 1      ; invoke SYS_EXIT (kernel opcode 1)
    int     80h

https://asmtutor.com/#lesson1

My commands :

Code:
nasm -f elf hello.s
ld -m elf_i386_fbsd -o hello -s hello.o
./hello

There are no errors and no output (no "Hello World!").
 
Do peek at calling conventions too. Modified example for FreeBSD:
Code:
section .data
    msg     db      'Hello World!', 0Ah

section .text
    global _start

_start:
    push dword 13
    push dword msg
    push dword 1
    mov eax, 4
    push eax
    int 0x80

    push 0
    mov eax, 1
    push eax
    int 0x80
 
FreeBSD provides kernel module - linux(4). You'd have to have it loaded and your binary brandelf(1) it. You should stick to FreeBSD native ABI.
No, this modified code would not yield the result you're hoping for on Linux. I don't want to go too much into the details here but do have a look on those calling conventions and ABI.
 
I've posted a 101 on how to use (Hello World style) nasm, fasm, as, gas32 and __asm__ in C. All have has been tested on a FreeBSD/amd64 machine.

Let me know, if you have any questions, or any ambiguity about that post.
 
Vigole, i tried some 64bit code but it gave bus error.
Then i found this which runs ok ,
 
Vigole, i tried some 64bit code but it gave bus error.
Because I don't know exactly, what error message you've received, I have to guess that you probably missed the sys.exit call, which is necessary at the end of .s file, or you have a typo -- or even missing part, in the %macro definition of the sys.exit in the .inc file. Both can produce the Bus Error (core dumped).
 
Back
Top