3
我想了解如何在C語言中嵌入彙編語言(在x86_64架構上使用gcc)。我編寫了這個程序來增加單個變量的值。但我得到垃圾價值作爲輸出。和想法爲什麼?通過嵌入式彙編語言遞增變量
#include <stdio.h>
int main(void) {
int x;
x = 4;
asm("incl %0": "=r"(x): "r0"(x));
printf("%d", x);
return 0;
}
感謝
更新該計劃是給在GCC 4.8.3預期的結果,但不是在GCC 4.6.3。我粘貼的非工作代碼的程序集輸出:
.file "abc.c"
.section .rodata
.LC0:
.string "%d"
.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
pushq %rbx
subq $24, %rsp
movl $4, -20(%rbp)
movl -20(%rbp), %eax
incl %edx
movl %edx, %ebx
.cfi_offset 3, -24
movl %ebx, -20(%rbp)
movl $.LC0, %eax
movl -20(%rbp), %edx
movl %edx, %esi
movq %rax, %rdi
movl $0, %eax
call printf
movl $0, %eax
addq $24, %rsp
popq %rbx
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
沒關係在我的x86_64 linux – 2014-09-03 05:37:25
@rakib你使用gcc嗎?爲了以防萬一,我使用gcc 4.6.3。 – Arani 2014-09-03 06:06:26
檢查gcc正在生成的彙編代碼。 'gcc -S test.c'將會在程序集列表中產生'test.s'。 – afenster 2014-09-03 06:19:18