我的最后一个项目是一个刽子手游戏。所选单词是从数据库中随机选取的。用户通过scanf
输入char
,然后必须通过汇编将其与所选单词进行比较。由于C
没有字符串变量,字符串只是一个字符数组,因此输入的char
在for loop
中,必须与数组中的每个索引char
进行比较。
现在传递汇编函数:int i(index#)、char string1指针(数组字)和char string2指针(用户输入)。
movb 8(%ebp), %ecx /*store i -> cx reg*/
movb 12(%ebp),%ebx /*store *string1 -> bh reg*/
movb 16(%ebp),%edx /*store (userinput)*string2 ->bl reg*/
movb (%ebx,%ecx,1),%al
movb (%ebx,%ecx,1), %ah
movl $0, %eax
cmpl %al, %ah
jne end
movl $1, %eax
我知道这两行是不正确的语法,我需要知道如何正确地偏移这些mov指令。此外,如果还有其他错误。这应该是在偏移后比较两个寄存器。我是组装新手。
movb (%bh,%cx,1),%edx
movb (%bl,%cx,1), %eax
edit:所以当比较这两个字符时,它现在只给我1的返回值,即使它们不同。
在32位模式下,不能使用%bh
、%bl
和%cx
等8位或16位寄存器作为地址和/或索引寄存器。您应该使用%ecx
、%ebx
和%edx
作为指针和索引寄存器,并使用%al
和%ah
从字符串中加载字节:
movl 8(%ebp),%ecx /* store 32 bit i -> ecx reg */
movl 12(%ebp),%ebx /* store string1 -> ebx reg */
movl 16(%ebp),%edx /* store (userinput) string2 -> edx */
movb (%ebx,%ecx,1),%al
movb (%edx,%ecx,1),%ah
cmpb %al,%ah
jne here
movl $0,%eax
jmp end
如果i
在C端具有类型unsigned char
,则必须通过以下方式对8位值进行零扩展:
movzbl 8(%ebp),%ecx /* store 8 bit i -> ecx reg */