2

I need to count amount of spaces in string using gasm. So, I have written with simple program, but comparison doesn't work.

.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

So, problem in this string cmpl $32,str(,%ecx,1) There I try to compare space (32 in ASCII) with 1 byte of str (I use %ecx as a counter for displacement, and took 1 byte). Unfortunately, I haven't found any example in Internet about symbol comparison in Gasm. I've tried to use gcc generated code, but I can't understand and use it.

4

1 回答 1

1

这永远不会返回 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 语法不太熟悉。但是,您应该了解您的问题是什么,以及如何解决它。

于 2013-05-25T15:25:09.127 回答