The Branch command for Assembly in Arm64 with GDB

I'm starting to learn assembly for ARMV8(Aarch64). I've got a Khadas Vim 3 board with a Amlogic Cortex A73 processor.

This is from chapter 6 of Thinkingeek's tutorial.

// branch.s
.text
.globl main
main:
  mov w0, #3   // w0 ← 3
  b jump       // branch to label jump
  mov w0, #4   // w0 ← 4
  jump:
    ret        // end function

Now to assemble it. I learned that the command

aarch64-linux-gnu-as -c -o first.o first.s

that I was using before is unnecessary since I am already on the arm64 platform. I can just use as in place. Now I did an as --help and looked up what i was using and what we are looking for is just -g for debugging and -o to specify the object file name. I'm not even sure -c is a thing.

$ as -g -o branch.o branch.s

Much easier. Now to link it but I learned a few things here too.

ld -e main -o branch branch.o

This compiles, but then seg faults. The -e main is because the code is using main instead of _start which ld is looking for.

$ gdb branch
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "aarch64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
    .

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from branch...
(gdb) run
Starting program: /home/khadas/Desktop/branch 

This GDB supports auto-downloading debuginfo from the following URLs:
  
Enable debuginfod for this session? (y or [n]) y
Debuginfod has been enabled.
To make this setting permanent, add 'set debuginfod enabled on' to .gdbinit.
Downloading separate debug info for system-supplied DSO at 0x7ff7fff000
                                                                                                                                                                                         
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()

So it looks like I'm doing it wrong. some some reading up on ld is needed. I'll just use gcc for now.

$ gcc -static -o branch branch.o
$ ./branch ; echo $?
3

works, and much easier to type.

Check out some other Bands on Bandcamp.com. Crazy Fingers (Vancouver 1991), Altogether Steve and the Mercenaries, and Hammy Ham Hands.

Proudly powered by an IDE, a Text Editor, and some Internet Searches.

2024 dispelled.ca end of file.