Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cc -o out out.s on macOS with M1 chip #76

Open
ge1mina023 opened this issue Jul 21, 2024 · 1 comment
Open

cc -o out out.s on macOS with M1 chip #76

ge1mina023 opened this issue Jul 21, 2024 · 1 comment

Comments

@ge1mina023
Copy link

ge1mina023 commented Jul 21, 2024

I have been learning the chapter of 04_Assembly. According to the README.md, after I typed cc -o comp1 -g cg.c expr.c gen.c interp.c main.c scan.c tree.c and./comp1 input01, I got an output 15 and a file out.s:

	.text
.LC0:
	.string	"%d\n"
printint:
	pushq	%rbp
	movq	%rsp, %rbp
	subq	$16, %rsp
	movl	%edi, -4(%rbp)
	movl	-4(%rbp), %eax
	movl	%eax, %esi
	leaq	.LC0(%rip), %rdi
	movl	$0, %eax
	call	printf@PLT
	nop
	leave
	ret

	.globl	main
	.type	main, @function
main:
	pushq	%rbp
	movq	%rsp, %rbp
	movq	$2, %r8
	movq	$3, %r9
	movq	$5, %r10
	imulq	%r9, %r10
	addq	%r8, %r10
	movq	$8, %r8
	movq	$3, %r9
	movq	%r8,%rax
	cqo
	idivq	%r9
	movq	%rax,%r8
	subq	%r8, %r10
	movq	%r10, %rdi
	call	printint
	movl	$0, %eax
	popq	%rbp
	ret

After that, I got an error. If i typed cc -o out out.s, I got :
out.s:5:8: error: unknown token in expression pushq %rbp ^ out.s:5:8: error: invalid operand pushq %rbp ^ out.s:6:7: error: unknown token in expression movq %rsp, %rbp ^ out.s:6:7: error: invalid operand movq %rsp, %rbp ^ out.s:7:12: error: unknown token in expression subq $16, %rsp ^ out.s:7:12: error: invalid operand subq $16, %rsp ^ out.s:8:7: error: unknown token in expression movl %edi, -4(%rbp) ^ out.s:8:7: error: invalid operand movl %edi, -4(%rbp) ^ out.s:9:9: error: unexpected token in argument list movl -4(%rbp), %eax ^ out.s:10:7: error: unknown token in expression movl %eax, %esi ^ out.s:10:7: error: invalid operand movl %eax, %esi ^ out.s:11:11: error: unexpected token in argument list leaq .LC0(%rip), %rdi ^ out.s:12:11: error: unknown token in expression movl $0, %eax ^ out.s:12:11: error: invalid operand movl $0, %eax ^ out.s:13:2: error: unrecognized instruction mnemonic, did you mean: casl? call printf@PLT ^ out.s:15:2: error: unrecognized instruction mnemonic leave ^ out.s:19:2: error: unknown directive .type main, @function ^ out.s:21:8: error: unknown token in expression pushq %rbp ^ out.s:21:8: error: invalid operand pushq %rbp ^ out.s:22:7: error: unknown token in expression movq %rsp, %rbp ^ out.s:22:7: error: invalid operand movq %rsp, %rbp ^ out.s:23:11: error: unknown token in expression movq $2, %r8 ^ out.s:23:11: error: invalid operand movq $2, %r8 ^ out.s:24:11: error: unknown token in expression movq $3, %r9 ^ out.s:24:11: error: invalid operand movq $3, %r9 ^ out.s:25:11: error: unknown token in expression movq $5, %r10 ^ out.s:25:11: error: invalid operand movq $5, %r10 ^ out.s:26:8: error: unknown token in expression imulq %r9, %r10 ^ out.s:26:8: error: invalid operand imulq %r9, %r10 ^ out.s:27:7: error: unknown token in expression addq %r8, %r10 ^ out.s:27:7: error: invalid operand addq %r8, %r10 ^ out.s:28:11: error: unknown token in expression movq $8, %r8 ^ out.s:28:11: error: invalid operand movq $8, %r8 ^ out.s:29:11: error: unknown token in expression movq $3, %r9 ^ out.s:29:11: error: invalid operand movq $3, %r9 ^ out.s:30:7: error: unknown token in expression movq %r8,%rax ^ out.s:30:7: error: invalid operand movq %r8,%rax ^ out.s:31:2: error: unrecognized instruction mnemonic cqo ^ out.s:32:8: error: unknown token in expression idivq %r9 ^ out.s:32:8: error: invalid operand idivq %r9 ^ out.s:33:7: error: unknown token in expression movq %rax,%r8 ^ out.s:33:7: error: invalid operand movq %rax,%r8 ^ out.s:34:7: error: unknown token in expression subq %r8, %r10 ^ out.s:34:7: error: invalid operand subq %r8, %r10 ^ out.s:35:7: error: unknown token in expression movq %r10, %rdi ^ out.s:35:7: error: invalid operand movq %r10, %rdi ^ out.s:36:2: error: unrecognized instruction mnemonic, did you mean: casl? call printint ^ out.s:37:11: error: unknown token in expression movl $0, %eax ^ out.s:37:11: error: invalid operand movl $0, %eax ^ out.s:38:7: error: unknown token in expression popq %rbp ^ out.s:38:7: error: invalid operand popq %rbp ^ .
I realized that my platform is arm. So, I downloaded x86_64-elf-gcc, and I got:
/opt/homebrew/opt/x86_64-elf-binutils/bin/x86_64-elf-ld: cannot find crt0.o: No such file or directory /opt/homebrew/opt/x86_64-elf-binutils/bin/x86_64-elf-ld: cannot find -lc: No such file or directory collect2: error: ld returned 1 exit status.

Is there any way to help me solve this?

My enviroment:
macOS 14.5 with m1
I guess qemu can be used to run my x86_64 code?

@AlejaDiez
Copy link

AlejaDiez commented Aug 30, 2024

You need to add the flag '-arch x86_64' to emulate the x86_64 architecture using Rosetta 2.
cc -o out -arch x86_64 out.s

You also need to modify the function name by adding "_" (_main) and remove ".type main, @function" and "@plt". Here is the asm code for test 01:

.text
    .LC0:
    .string"%d\n"

_printint:
    pushq   %rbp
    movq    %rsp,       %rbp
    subq    $16,        %rsp
    movl    %edi,       -4(%rbp)
    movl    -4(%rbp),   %eax
    movl    %eax,       %esi
    leaq    .LC0(%rip), %rdi
    movl    $0,         %eax
    call    _printf
    nop
    leave
    ret

    .globl _main

_main:
    pushq   %rbp
    movq    %rsp,       %rbp
    movq    $2,         %r8
    movq    $3,         %r9
    movq    $5,         %r10
    imulq   %r10,       %r9
    addq    %r9,        %r8
    movq    $8,         %r9
    movq    $3,         %r10
    movq    %r9,        %rax
    cqo
    idivq   %r10
    movq    %rax,       %r9
    subq    %r9,        %r8
    movq    %r8,        %rdi
    call    _printint
    movl    $0,         %eax
    popq    %rbp
    ret

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants