Assembly file won't "link"

Thread Starter

arduinolego611

Joined Jan 23, 2022
24
so I bought a book that is teaching me how to run an assembly language file

ive successfully converted myexit.s into myexit.o

next, it says to "link" the file -
Code:
`ld myexit.o -o myexit`
when I enter that command, the error reads -

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

the book says the lessons can be used by a Mac user, but it looks like there may in fact be some syntax issues. any help is appreciated.

by the way, this is the code that lives in that file:

Code:
 `.text

.global _start

_start:

      movq $60, %rax

      movq $3, %rdi

      syscall`
(and yes .text works whereas .section .text doesn't, that was already discovered)
 

Ya’akov

Joined Jan 27, 2019
5,657
So here's the problem. MacOS is very different from Linux and while the Docker container was supposed to deal with that, it didn't. Your code also has syntax errors in it. I don't know what it is supposed to be. Here is something that successfully assembles and links on my Mojave machine using NASM which can be installed with MacPorts or Brew

Netwide Assembler:
global start

section .text

start:
    mov     rax, 0x2000004 ; write
    mov     rdi, 1 ; stdout
    mov     rsi, msg
    mov     rdx, msg.len
    syscall

    mov     rax, 0x2000001 ; exit
    mov     rdi, 0
    syscall


section .data

msg:    db      "Hello, world!", 10
.len:   equ     $ - msg
I saved this as "hw.asm".

Then invoke NASM like so:
nasm -f macho64 hw.asm

Then ld, which is picky:
ld -macosx_version_min 12.2.1 -static -o hw hw.o

The key things are:

-macosx_ver_min which must be set to something ≤ your MacOS version, check "About This Mac" in the Apple menu
-static to prevent attempting to create a dynamic executable (usually only used for kernel code but here just to simplify)

Then, if all went well ./hw should execute and output "Hello, world!"

I think you either need to move to Linux or find a MacOS specific source. Age matters too, things have changed in various ways pretty quickly.

Good luck.
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
24
So here's the problem. MacOS is very different from Linux and while the Docker container was supposed to deal with that, it didn't. Your code also has syntax errors in it. I don't know what it is supposed to be. Here is something that successfully assembles and links on my Mojave machine using NASM which can be installed with MacPorts or Brew

Netwide Assembler:
global start

section .text

start:
    mov     rax, 0x2000004 ; write
    mov     rdi, 1 ; stdout
    mov     rsi, msg
    mov     rdx, msg.len
    syscall

    mov     rax, 0x2000001 ; exit
    mov     rdi, 0
    syscall


section .data

msg:    db      "Hello, world!", 10
.len:   equ     $ - msg
I saved this as "hw.asm".

Then invoke NASM like so:
nasm -f macho64 hw.asm

Then ld, which is picky:
ld -macosx_version_min 12.2.1 -static -o hw hw.o

The key things are:

-macosx_ver_min which must be set to something ≤ your MacOS version, check "About This Mac" in the Apple menu
-static to prevent attempting to create a dynamic executable

Then, if all went well ./hw should execute and output "Hello, world!"

I think you either need to move to Linux or find a MacOS specific source. Age matters too, things have changed in various ways pretty quickly.

Good luck.
you know, I thought I had moved past this part but hearing your response, I know what the problem is.

so after I downloaded docker I was supposed to run the following command to get an image created by the author of the book. it reads:

Code:
Docker run -it —rm —mount “type=bind,src=`pwd`,target=/my-code”johnnyb61820/linux-assembly
^ there are two short dashes before rm and mount, the website text editor switched them to long dashes
I had this error:


Docker run -it --rm --mount “type=bind,src=$(pwd),target=/my-code”johnnyb61820/linux-assembly


invalid argument "“type=bind,src=/users/hill/dockerdir,target=/my-code”johnnyb61820/linux-assembly" for "--mount" flag: unexpected key '“type' in '“type=bind'


See 'docker run --help'.

like I said I thought I could just bypass this part, but now I feel stupid, its very obvious this has been my problem the whole time. what im trying to run is totally the wrong thing without that image. is there any way you could help me move past this error message and download the image that was made for this code?? its from docker, and like I said, the instructions are to just type that bit of code into the command line. I copied it correctly and tried to run it in a directory I made just for docker projects


***I made some edits to this response...just so you know if something doesn't make sense
 
Last edited:
Top