晚上好,我正在尝试将我的代码从C转换为程序集IA32:
int i=0;
char s[3]; char c[n];
for(i=0;i<3;i++){
s[i]=c[i];
}
n是一个从用户接收值的char。当这个“for”它正在执行“c”时已经充满了chars。我想,正如你所看到的,用“c”的三个第一个条目填写“s”。我把这段代码翻译成这样的程序集:
.data
is: .asciz "%s"
id: .asciz "%d"
.bss
.comm c,500,1
.comm s,500,1
.comm n,4,4
.global main
main:
#asking for string to fill "c"
pushl $c
pushl $is
call scanf
addl $8,%esp
#asking for integer to know how long is c
pushl $n
pushl $id
call scanf
addl $8,%esp
movl $0,%eax
for:
cmpl $3,%eax
jge endfor
movb c(%eax),%cl
movb %cl,s(%eax)
incl %eax
jmp for
endfor:
movl $0,%eax
pushl %eax
pushl $s
pushl $is
call printf
addl $8,%ebp
popl %eax
事情就是当我执行这段代码时它根本不打印任何东西,就像“s”数组它是空的,我已经尝试了几件事,到目前为止还没有任何工作。任何集会天才能帮助我吗?
:-)谢谢!
答案 0 :(得分:1)
以下为我工作正常:
.data
is: .asciz "%s"
id: .asciz "%d"
.bss
.comm c,500,1
.comm s,500,1
.comm n,4,4
.text
.global main
main:
#asking for string to fill "c"
pushl $c
pushl $is
call scanf
addl $8,%esp
#asking for integer to know how long is c
pushl $n
pushl $id
call scanf
addl $8,%esp
movl $0,%eax
for:
cmpl $3,%eax
jge endfor
movb c(%eax),%cl
movb %cl,s(%eax)
incl %eax
jmp for
endfor:
movl $0,%eax
pushl %eax
pushl $s
pushl $is
call printf
addl $8,%esp
popl %eax
ret
addl $8,%ebp
调用之后,addl $8,%esp
近端应该printf
来修复堆栈指针
ret
main
我不认为我改变了其他任何东西。
汇编并链接:
as -g -32 test.s -o test.o
gcc -g -m32 test.o
输入:
abc 3
输出:
abc