Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
汇编语言-c语言和助记符_C_Assembly_Mnemonics - Fatal编程技术网

汇编语言-c语言和助记符

汇编语言-c语言和助记符,c,assembly,mnemonics,C,Assembly,Mnemonics,我用c语言写了一个简单的程序,经典的helloworld。我想知道当编译器将它翻译成汇编代码时,它看起来是什么样子 我使用MinGW和命令: gcc-S hellow.c 当我打开此文件时,我希望它至少与直接在汇编中编写的hello world程序有些相似,即: jmp 115 db 'Hello world!$' (db = define bytes) -a 115 mov ah, 09 (09 for displaying strings ... ah = 'command registe

我用c语言写了一个简单的程序,经典的helloworld。我想知道当编译器将它翻译成汇编代码时,它看起来是什么样子

我使用MinGW和命令:

gcc-S hellow.c

当我打开此文件时,我希望它至少与直接在汇编中编写的hello world程序有些相似,即:

jmp 115
db 'Hello world!$' (db = define bytes)
-a 115 
mov ah, 09 (09 for displaying strings ... ah = 'command register')
mov dx, 102 (adress of the string)
int 21
int 20
相反,它看起来是这样的:

   .file    "hellow.c"

.def    ___main;    
.scl    2;  
.type   32; 
.endef
.section 
.rdata,"dr"
LC0:

.ascii "Hello world!\0"

.text
.globl  _main

.def    _main;  
.scl    2;  
.type   32; 
.endef
_main:
 LFB6:

.cfi_startproc
pushl   %ebp

.cfi_def_cfa_offset 8

.cfi_offset 5, -8
movl    %esp, %ebp

.cfi_def_cfa_register 5
andl    $-16, %esp
subl    $16, %esp
call    ___main
movl    $LC0, (%esp)
call    _puts
movl    $0, %eax
leave

.cfi_restore 5

.cfi_def_cfa 4, 4
ret

.cfi_endproc
LFE6:

.def    _puts;  
.scl    2;  
.type   32; 
.endef
我对汇编语言知之甚少,但我确实认识到所谓的助记符,如ADD、POP、PUSH、MOV、JMP、INT等。在c编译器生成的代码中看不到这些助记符


我误解了什么?

这准备了调用函数main的参数,该函数可能完成C程序所需的所有初始设置

andl    $-16, %esp
subl    $16, %esp
call    ___main

这将准备参数并调用函数_puts。LC0是包含要打印的字符串的符号

movl    $LC0, (%esp)
call    _puts
这将准备main和returns的返回值

movl    $0, %eax
leave
ret

这将为调用函数_main做准备,该函数可能完成C程序所需的所有初始设置

andl    $-16, %esp
subl    $16, %esp
call    ___main

这将准备参数并调用函数_puts。LC0是包含要打印的字符串的符号

movl    $LC0, (%esp)
call    _puts
这将准备main和returns的返回值

movl    $0, %eax
leave
ret
示例代码使用Intel语法,而gcc的标准输出是AT&T语法。你可以通过使用

gcc -S hellow.c -masm=intel
结果输出应该看起来更熟悉

然而,如果编译器生成了源代码,那么它看起来就大不相同了,那么您将手工编写什么

如果您为DOS编译,则会使用int,但即使如此,这些调用也会封装在C标准函数中,就像本例中的puts一样。

示例代码使用Intel语法,而gcc的标准输出是AT&T语法。你可以通过使用

gcc -S hellow.c -masm=intel
结果输出应该看起来更熟悉

然而,如果编译器生成了源代码,那么它看起来就大不相同了,那么您将手工编写什么


如果您为DOS编译,则会使用int,但即使如此,这些调用也会封装在C标准函数中,如本例中的puts。

基本上,C程序使用C标准运行时库进行I/O,而您的原始程序集似乎使用DOS中断等。基本上,C程序使用C标准运行时库进行I/O,而您的原始程序集似乎使用DOS中断或其他方式。有时您的代码会消失,但当尝试以这种方式学习时,最好尝试使用优化-O2或编译器想要的任何方式进行编译。如果没有优化,将有更多的开销和代码需要尝试理解。不过,有时你的死代码会被删除,你必须理解这意味着什么……有时你的代码会消失,但当你尝试用这种方式学习时,最好尝试使用优化(O2)或你的编译器想要的任何东西进行编译。如果没有优化,将有更多的开销和代码需要尝试理解。不过,有时你的死代码会被删除,你必须明白这意味着什么。。。