繁体   English   中英

如何在Gasm(Gnu汇编程序)中比较符号和字符串?

[英]How to compare a symbol with string in Gasm (Gnu Assembler)?

我需要使用gasm计算字符串中的空格数量。 因此,我已经编写了简单的程序,但是比较不起作用。

.section .data
  str:
    .string " TEst   string wit h spaces   \n"

.section .text
.globl _start
_start:

movl $0,%eax # %eax - amount of spaces
movl $0,%ecx # Starting our counter with zero

loop_start:
  cmpl $32,str(,%ecx,1)  # Comparison (this is never true)
  jne sp
  incl %eax # Programm never goes there
  incl %ecx
  jmp loop_start
sp:
  cmpl $0X0A,str(,%ecx,1) #Comparison for the end of string
  je loop_end #Leaving loop if it is the end of string
  incl %ecx
  jmp loop_start
loop_end:
  movl (%eax),%ecx  # Writing amount of spaces to %ecx
  movl $4,%eax
  movl $1,%ebx
  movl $2,%edx
  int $0x80

  movl $1,%eax
  movl $0,%ebx
  int $0x80

因此,此字符串中的问题cmpl $32,str(,%ecx,1)我尝试将空间(ASCII中的32)与1个字节的str进行比较(我将%ecx用作位移的计数器,并占用了1个字节)。 不幸的是,我没有在Internet上找到有关Gasm中符号比较的任何示例。 我尝试使用gcc生成的代码,但是我无法理解和使用它。

这永远不会返回true,我想我知道为什么:

cmpl $32,str(,%ecx,1)

因为您正在将立即数与内存地址进行比较,所以汇编器无法知道两个操作数的大小。 因此,可能假设每个参数都是32位,但是您想比较两个8位值。 您需要某种方式来明确声明您正在比较字节。 我的解决方案是:

mov str(,%ecx,1), %dl  # move the byte at (str+offset) into 'dl'
cmp $32, %dl           # compare the byte 32 with the byte 'dl'.
# hooray, now we're comparing the two bytes!

可能有更好的方法来显式比较字节,而且我可能在某个地方犯了一个愚蠢的错误; 我对AT&T语法不太熟悉。 但是,您应该了解问题所在以及如何解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM