intel記法とat&t記法

main:
        mov rbp, rsp
        mov %rsp, %rbp
        
        mov rax, 8
        mov $8, %rax

        mov [rbp + rcx * 4 - 8], rax
        mov %rax, -8(rbp, rcx, 4)
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv){
    if(argc != 2) {
        fprintf(stderr, "引数の個数が正しくありません\n");
        return 1;
    }

    printf(".intel_syntax noprefix\n");
    printf(".global main\n");
    printf("main:\n");
    printf("    mov rax, %d\n", atoi(argv[1]));
    printf("    ret\n");
    return 0;
}

$ cc -o 9cc 9cc.c
$ ./9cc 123 > tmp.s

.intel_syntax noprefix
.global main
main:
    mov rax, 123
    ret

テストスクリプト

#!/bin/bash
assert(){
    expected="$1"
    input="$2"

    ./9cc "$input" > tmp.s
    cc -o tmp tmp.s
    ./tmp
    actual="$?"

    if ["$actual" = "$expected"]; then
        echo "input => $actual"
    else
        echo "input => $expected expected, but got $actual"
        exit 1
    fi
}

assert 0 0
assert 42 42

echo OK

Makefile

CFLAGS=-std=c11 -g static

9cc: 9cc.c11
test: 9cc
	./test.sh

clean:
	rm -f 9cc *.o *~ tmp*

.PHONY: test clean