NASM Program error

Hi I tried to create a asm code which will just print "My name" and "id".

; To assemble and run:
; nasm -f bin -o boot.bin boot.asm
; dd if=boot.bin of=/dev/sdb bs=512 count=1
; (replace /dev/sdb with the appropriate device file for your USB drive)

section .text
    global _start

_start:
    ; Print "Hello, my name is "
    mov rax, 0x1301
    mov rbx, 0x0003
    mov rcx, 22
    mov rdx, msg ;
    int 0x10    ;call the kernel

    ; Print " and my roll number is "
    mov rax, 0x1301
    mov rbx, 0x0003
    mov rcx, 23
    mov rdx, msg2 
    int 0x10

    ; Print "123456"
    mov rax, 0x1301
    mov rbx, 0x0003
    mov rcx, 6
    mov rdx, msg3
    int 0x10

    ; Wait for a key press
    mov rax, 0x16
    int 0x10

    ; Return to boot loader
    mov rax, 0x0000
    int 0x18

section .data
    msg db "Hello, my name is ManuJain " ;message is kind of a variable, db - define bits
    msg2 db " My College number is  "
    msg3 db "1234567"

this is command

ld test.o -o test -demangle -dynamic -macosx_version_min 11.0 -L/usr/local/lib -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -lSystem -no_pie

When I running this on terminal I am getting this message

Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64

When I running this on terminal I am getting this message

That isn’t going to work. The assembly language code you posted looks like it was written to run in the MS-DOS environment. Specifically:

  • Its entry point is the first instruction.

  • It uses int instructions to call the OS.

macOS command-line tools don’t work that way. Specifically:

  • Their entry point is defined by a Mach-O load command (LC_MAIN) which usually points to start which then bounces through to main.

  • They call the OS by calling functions in libSystem, accessed using the dynamic linker (dyld).

You need to decide on your path forward here:

  • If you want to run your command from Terminal, you’ll have to write code for that environment.

  • If you want to run your code in the MS-DOS environment, you’ll need to actually do that, but also adjust your build system to not create a Mach-O.

I should stress that both of these are hard. I don’t have any useful info to share about MS-DOS programming but, with regards that first option, writing a useful macOS command-line tool using only assembly language is a serious challenge. If you want to play around with assembly language, it’s much easier to write your main function in C and then have it call your assembly language function. That allows you to focus on the assembly language rather than get distracted by packaging.

For example:

% cat main.c
#include <stdio.h>

extern void test();

int main(int argc, char ** argv) {
    printf("will call `test`\n");
    test();
    printf("did call `test`\n");
    return 0;
}
% cat test.s
.text

    .globl _test

_test:
    ret
% clang main.c test.s
% ./a.out            
will call `test`
did call `test`

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NASM Program error
 
 
Q