Post

assembly

RISC-V

The best way to try out RISC-V is by riscv-tools. Checkout out git organization riscv-software-src. It contains RISC-V installer, compiler and emulator.

Using Macbook M1 as an example,

1
2
brew tap riscv-software-src/riscv
brew install riscv-tools

Write a simple C program

1
2
3
4
5
6
#include <stdio.h>

int main() {
  printf("hello world\n");
  return 0;
}

Then compile it

1
2
3
riscv64-unknown-elf-gcc -S hello.c
riscv64-unknown-elf-gcc -c hello.s -o hello.o
riscv64-unknown-elf-gcc hello.o -o hello

and run it

1
2
3
$ spike pk hello
bbl loader
hello world

Registers and instruction set

See manual

amd64

Registers

I have a hard time of remembering the meaning of registers in amd64 architecture until I found this post. Just cite the summary below:

EAX, EBX, ECX, EDX = A, B, C, D; Note that the ‘A’ register holds function return values

ESI, EDI = Source, Destination (for string operations) - ECX may be the counter and EAX may used, too.

ESP, EBP = Stack Pointer, Base Pointer

EIP = Instruction Pointer

CS, DS, SS, ES, FS, GS = Code, Data, Stack, and Extra segments, followed by F and G Segments

Syntax

x86 assembly language has two main syntax branches: Intel syntax and AT&T syntax. Check wiki for details of their differences. Gnu assembler (gas or as) is probably the most popular one that uses AT&T syntax. And nasm is most popular one uses Intel syntax.

RIP-relative addressing

1
 lea rsi, [rel msg]

References

  • https://cs.lmu.edu/~ray/notes/nasmtutorial/

aarch64

Aarch64 is just arm64. The instruction set used in aarch64 is called A64.

as

CFI (Call Frame Information) directives

Assembly Sections

  • .data
  • .text
  • .bss

FAQ

Difference between stack pointer and frame pointer

TODO: write it up.

How to compile with frame pointer enabled/disabled?

This post is licensed under CC BY 4.0 by the author.