【问题标题】:Problem with comparing two characters, one of which I get from a string with an indirect addressing比较两个字符时出现问题,其中一个字符是从带有间接寻址的字符串中得到的
【发布时间】:2019-10-09 21:44:20
【问题描述】:

所以我是组装新手,我需要一些内存地址方面的帮助。 (我认为它们是内存地址,但我不确定)所以,我有一个字符串,我想找到字符 % 出现在哪里。

所以,我做了一个循环,将值 37 移动到一个寄存器中(37 是 % 的 ascii 值),首先我尝试将该寄存器与 mystring(%counter) 进行比较,其中 counter 是我使用的寄存器作为索引,每次循环结束时将其递增 1。然后,我运行调试工具,发现比较总是错误的。因此,我将值 mystring(%counter) 移动到另一个寄存器中,当我再次运行 gdb 时,我看到那里有一个非常大的数字。所以,我认为这是一个内存地址或其他东西,并试图将 mystring(%counter) 加载到寄存器中。这也没有奏效。

.text

mystring:  .asciz  "asdb%asd%af "  //that's how I declared my string


loop1:

     cmpq %r9 , %r14  //in r9 I have the length of the string, r14 is the 
                      //index

     jl if1



if1: 


     movw $37 , %bx

     leaw mystring(%r14)  , %ax
     cmpw %ax , %bx
     je something
     incq %r14
     jmp loop1

所以即使 mystring(%r14) 指向一个 % ,如果相等的跳转永远不会发生,当我运行调试器时,它在 ax 中显示了一个大数字。 (我还尝试调整寄存器的大小,因为我希望这会以某种方式改变值,这就是我使用 w 后缀的原因。) P.S 这是我在这里的第一篇文章,所以如果我不遵守某些规则或其他内容,请不要太苛刻。 :)

【问题讨论】:

    标签: assembly x86-64 att


    【解决方案1】:

    这段代码有几个问题。

    1. 字符串由 8 位 ASCII 字符组成,因此代码应使用 8 位比较。
    2. 它永远不会从字符串中读取字符的值。
    3. 如果找不到 '%' 字符,它永远不会退出循环。

    这是修复了这些问题的代码。

    .text
    
    mystring:  .asciz  "asdb%asd%af "  //that's how I declared my string
    
    
    
    loop1:
         cmpq %r9, %r14   //in r9 I have the length of the string, r14 is the 
                          //index
         jge endloop1
    
         movb $37, %bl
         movb mystring(%r14), %al
         cmpb %bl, %al
         je something
         incq %r14
         jmp loop1
    
    endloop1:
    

    我有一些额外的建议来改进这段代码:

    1. 将循环检查放在循环的末尾。
    2. movbcmpb 指令替换为一条指令。

           cmpq %r9, %r14    // This check may not be necessary if the string
           jge skiploop1     // length is known to be greater than 0.
      
       loop1:
           cmpb $37, mystring(%r14)
           je something
           incq %r14
           cmpq %r9, %r14   // r9 is the length of the string, r14 is the 
                            // index
           jl loop1
      
       skiploop1:
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多