【发布时间】:2015-05-03 18:29:55
【问题描述】:
我必须在汇编中编写一个函数来完成以下 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 中。所以我认为它可以将每个 char 值与其从 65 到 90 和从 97 到 122 开始的 ascii 值进行比较。但是当我开始编译程序集时,它不断收到错误“'cmp'的操作数大小不匹配”指令@ 987654323@。 你能帮帮我吗?
【问题讨论】:
-
为了可读性,请缩进 C 源代码。建议在左大括号 '{' 后留 4 个空格,在右大括号 '}' 前取消缩进
-
我认为入口点需要声明为 .global 才能调用