【问题标题】:Cmp instruction mismatchCmp 指令不匹配
【发布时间】: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 才能调用

标签: c assembly x86 att


【解决方案1】:
cmp  (%eax,%edx,1), $0

你需要反转操作数。

cmp  $0, (%eax,%edx,1)

您是否注意到您编写了无限循环?
您使用movl $0x0,-0x4(%ebp) 设置了一个变量,但您忘记在整个代码中更改它的值!

由于您的输入字符串是 ASCIIZ,您不应该与 CL 比较而不是与 ECX 比较吗?

【讨论】:

  • 是的,你是对的。有效。是的,我已经更改了循环,感谢您的提醒。如果我将 de %ecx 更改为 %cl 则会出现问题,我无法获得使用 add $0x1 (%ebx,%ecx,4) 获得的 int 数组的位置。我说 %cl 不是有效的基数/索引
  • 将 %ecx 更改为 %cl 的建议仅适用于两个比较指令! cmp (%eax,%edx,1), %clcmp (%eax,%edx,2), %cl %ecx 的所有其他用途都可以保存。
猜你喜欢
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
相关资源
最近更新 更多