0

I am new to assembly and would like to learn arm assembly on Mac M1, which is my dev environment. I need help finding many online resources, so I'm here. My code is as follows (the file is called Test5.s).

When I executed the program, I got the following error:

bus error ./Test5

Here below is the code of the program

`// Assembler program to print " Hello World!"
// to stdout
// X0-X2 - parameters to linux functions services
// x16 - linux function number
// syscall write(int fd, const void *buf, size_t count)

.text

// Our application's entry points.

.global _main
.align  2

_main:
    mov     X0, #1                      ; arg [0] = 1 = StdOut
    ldr     X1, =helloworld             ; arg [1] = String to print
    mov     X2, #16                     ; arg [2] = lenght of our string




    mov     X16, #4                 ; Unix write system call
    svc     #0x80                   ; Call kernel to output the string program

// Setup the parameters to exit the program
// and then call the kernet to do it.
    mov     X0,     #0                  ; Use 0 return code
    mov     X16,    #1                  ; Unit exit system call
    svc     #0x80                       ; Call kernel to end program


.data
helloworld: .ascii  "Hello M1-World!\n";`


Here is the command used for the compilation and the linking in my makefile

Test5: Test5.o
    ld -o Test5 Test5.o -lSystem -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _main -arch arm64
    
Test5.o: Test5.s
    as -arch arm64 -o Test5.o Test5.s

clean:
    rm -f *.o
    rm -f Test5

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 2
    See the second example in the linked duplicate. MacOS doesn't do relocations in the text section, so you can't do `ldr x1, =helloworld`. Instead, `adrp x1, helloworld@GOTPAGE` followed by `add x1, x1, #helloworld@GOTPAGEOFF`. For a small enough program, `adr x1, helloworld` may also work. – Nate Eldredge Dec 27 '22 at 17:01

0 Answers0