比较程序集 x86 中的数字和变量

compare number and variable in assemblyx86

此代码逐个字符地从输入文件中读取数据并将其写入另一个文件。 一旦找到值为 3 的字符,它应该停止读写。 我说应该是因为程序一旦找到等于 3 的值就不会停止,而是继续读取直到文件末尾。

输入文件如下:2 4 5 3 1 8

我的代码是:

.section .data

  varInputHandle: .long 100
  varOutputHandle: .long 100
  varExitCode: .long 1
  cont: .long 1
.section .bss
  .lcomm varBuffer, 1        

.section  .text              # declaring our .text segment
  .globl _start              # telling where program execution should start

_start:

    popl %eax       # Get the number of arguments
    popl %ebx       # Get the program name

    # open input file first 
    popl %ebx       # Get the first actual argument - file to read
    movl , %eax       # open
    movl [=10=], %ecx       # read-only mode
    int [=10=]x80
    movl %eax, varInputHandle   #store input file handle to memory

    #open output file, make it writable, create if not exists
    popl %ebx           # Get the second actual argument - file to write
    movl , %eax       # open 
    movl 01, %ecx    # create flag + write only access (if google is telling me truth)
    movl 66, %edx    #permissions for out file as rw-rw-rw-
    int [=10=]x80
        movl %eax, varOutputHandle #store output file handle to memory

contToZero:
movl [=10=], cont
processingLoop:
    incb cont
    #read single char to varBuffer
    movl , %eax
    movl varInputHandle, %ebx
    movl $varBuffer, %ecx
    movl , %edx
    int [=10=]x80

    #if no char was read (EOF?), jmp finishProcessing
    cmpl [=10=], %eax
    jz finishProcessing # looks like total success, finish cleanly

    cmpl , varBuffer   // this instruction is never true, don't know why
    je exitToOs
    #write it
    movl , %eax       
    movl varOutputHandle, %ebx     # file_descriptor
    movl $varBuffer, %ecx  
    movl , %edx
    int [=10=]x80

    # done, go for the next char
    goForTheNextChar:
    jmp processingLoop

finishProcessing:
    movl [=10=], varExitCode #everything went OK, set exit code to 0

exitToOs:
    movl varOutputHandle, %ebx     # file_descriptor
    movl varInputHandle, %ebx    
    movl , %eax
    movl varExitCode, %ebx
    int [=10=]x80

closeFile:
    cmpl $-1, %ebx
    movl , %eax  #sys_close
    int [=10=]x80

cmpl , varBuffer 似乎永远不会是真的因此我无法跳转到 exitToOs。

在你的代码中你有:

 cmpl , varBuffer

这不可能是真的,因为您的文件中没有二进制数据。

当您逐个字符读取时,您正在读取 ASCII 值。为了正确地进行这种比较,您必须做以下两件事之一:

  1. 将读取的字符从 ASCII 转换为十进制
  2. 将读取值与 ASCII 值进行比较

由于您依靠 $0 来识别何时读取了零字节,我建议您采用(更简单的)方法来检查您想要查找的 ASCII 值。在您的情况下,这将是:

 cmpb $'3', varBuffer     # Compare character to 0x33 / 51 / "3"