【发布时间】:2022-01-12 07:15:14
【问题描述】:
今天,我决定制作一个(非常)简单的操作系统。我不想直接使用汇编,因为我认为它对我来说太乱了。但是,我知道一点C,我渴望学习更多。我做了一个简单的程序来打印一个字符串,我想把它编译成汇编。我用这个命令编译成程序集:
gcc -S foo.c
这是我的 C 代码:
#include<stdio.h>
int main() {
printf("Hi!!!");
}
但是当我尝试运行输出文件 (foo.s) 时,出现以下错误:
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
这是我为编译汇编代码而运行的命令:
nasm -f bin -o foo.bin foo.s
这是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
有人请帮帮我!
【问题讨论】:
-
你想用
foo.bin做什么? -
@littleadv 我将使用:
dd将.bin文件转换为foo.flp,这样我就可以使程序可启动。 -
您的程序将无法启动,因为它使用了标准库。
-
@DYZ 实际上我只是测试了它。我怎样才能让它启动呢?
-
当你说你“试图运行输出文件 foo.s”时,你到底做了什么?您不能“运行” .s 文件,因为它是源文件。它必须首先组装和链接。