Issue
Today, I have decided to make a (very) simple OS. I don't feel like using assembly directly because I think that it is simply too messy for me. But, I know a little C, and I am eager to learn it more. I made a simple program to print a string and I want to compile it to assembly. I used this command to compile into assembly:
gcc -S foo.c
Here is my C code:
#include<stdio.h>
int main() {
printf("Hi!!!");
}
But when I try to run the output file (foo.s), I get these errors:
foo.s:1: error: parser: instruction expected
foo.s:2: warning: label alone on a line without a colon might be in error [-w+label-orphan]
foo.s:3: error: parser: instruction expected
foo.s:5: error: parser: instruction expected
foo.s:6: warning: label alone on a line without a colon might be in error [-w+label-orphan]
foo.s:7: error: parser: instruction expected
foo.s:8: error: parser: instruction expected
foo.s:11: warning: label alone on a line without a colon might be in error [-w+label-orphan]
foo.s:12: error: parser: instruction expected
foo.s:13: error: parser: instruction expected
foo.s:14: error: parser: instruction expected
foo.s:15: error: expression syntax error
foo.s:16: error: parser: instruction expected
foo.s:17: error: parser: instruction expected
foo.s:18: error: parser: instruction expected
foo.s:20: error: label `movl' inconsistently redefined
foo.s:18: info: label `movl' originally defined here
foo.s:20: error: parser: instruction expected
foo.s:21: error: parser: instruction expected
foo.s:22: error: parser: instruction expected
foo.s:24: warning: label alone on a line without a colon might be in error [-w+label-orphan]
foo.s:26: error: parser: instruction expected
foo.s:27: error: parser: instruction expected
foo.s:28: error: parser: instruction expected
This is the command I ran to compile the assembly code:
nasm -f bin -o foo.bin foo.s
This is the assembly code in foo.s
:
.file "foo.c"
.text
.section .rodata
.LC0:
.string "Hi!!!"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
leaq .LC0(%rip), %rdi
movl $0, %eax
call printf@PLT
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Debian 10.2.1-6) 10.2.1 20210110"
.section .note.GNU-stack,"",@progbits
Someone please help me!
Solution
gcc
output does not use NASM syntax. You should use as
to assemble it into an object file, and then link with the standard library:
> as -o foo.o foo.s
> gcc foo.o # Link
> ./a.out
Hi!!!
Answered By - DYZ Answer Checked By - Pedro (WPSolving Volunteer)