我必须在程序集中编写一个函数来完成以下c++代码。
int main(){
int hist[26]={0};
int i;
charHistogram("This is a string", hist);
for (i=0; i<26; i++ )
printf("%c:%d ", i+’a’, hist[i] );
printf("\n");
}
return 0;
}这是我写的汇编代码:
_charHistogram:
push %ebp
movl %esp,%ebp
subl $0x10,%esp
movl $0x0,-0x4(%ebp)
movl $0x41,-0x8(%ebp)
condFor1:
movl -0x4(%ebp),%edx
movl 0X8(%ebp),%eax
cmp (%eax,%edx,1), $0
je exit
condFor2:
cmpl $0x5a,-0x8(%ebp)
jg condFor1
if1:
movl -0x8(%ebp), %ecx
cmp (%eax,%edx,1), %ecx
jne if2
subl $0x41,%ecx
movl 0xc(%ebp), %ebx
add $0x1, (%ebx,%ecx,4)
add 0x20,%ecx
inc %ecx
inc %edx
jmp condFor1
if2:
add 0x20,%ecx
cmp (%eax,%edx,2), %ecx
jne condFor1
subl $0x41,ecx
movl 0xc(%ebp), %ebx
add $0x1, (%ebx,%ecx,4)
add 0x20,%ecx
inc %ecx
inc %edx
jmp condFor1
exit:
leave
ret基本上,在程序集中编写的函数必须计算给定字符串上出现的字母数,并将其存储在int数组hist中。所以我想它可以把每个字符值和它的ascii值进行比较,从65到90,从97到122。但是,当我开始编译程序集时,它会得到指令cmp (%eax,%edx,1), $0的错误“操作数大小不匹配的'cmp'”。你能帮帮我吗?
发布于 2015-05-03 19:04:15
cmp (%eax,%edx,1), $0你需要逆转操作数。
cmp $0, (%eax,%edx,1)你有没有注意到你已经设计了无尽的循环?
您使用movl $0x0,-0x4(%ebp)设置了一个变量,但是您忘记了在整个代码中更改它的值!
由于您的输入字符串是ASCIIZ,所以不应该与CL进行比较,而不是与ECX进行比较吗?
https://stackoverflow.com/questions/30017733
复制相似问题