【问题标题】:assembly x86: simple string comparison routine汇编 x86:简单的字符串比较例程
【发布时间】:2014-03-18 15:02:43
【问题描述】:

这个例程有什么问题?它不适用于下面的给定值。我在 shell 中将它用于一个简单的内核,但我不知道为什么它不起作用。

mov si, buffer                 ; buffer value from user input: 'help'
mov di, shell_command_help     ; shell_command_help db 'help',0
call os_compare_string 

; ----------------------------------------------------------
; Routine: Compare equality of two strings
; Waits for a complete string of user input and puts it in buffer.
; Sensitive for backspace and Enter buttons
; Input    1. String1 in SI    2. String2 in DI
; Output   1. result in carry flag
; ----------------------------------------------------------
os_compare_string:

  .compare_next_character:      ; a loop that goes character by character
    mov al, [si]      ; focus on next byte in si

    mov bl, [di]      ; focus on next byte in di
    cmp al, bl
    jne .conclude_not_equal       ; if not equal, conclude and exit

                      ; at this point we know the two bytes are equal
    cmp bl, 0         ; did we just compare two zeros?
    je .conclude_equal         ; if yes, we've reached the end of the strings. They are equal.

                      ; increment counters for next loop
    inc di
    inc si
    jmp .compare_next_character

  .conclude_equal:
    stc              ; sets the carry flag (meaning that they ARE equal)
    jmp .done

  .conclude_not_equal:
    clc              ; clears the carry flag (meaning that they ARE NOT equal)
    jmp .done

  .done:
    xor cl, cl
    ret

【问题讨论】:

  • 对我来说很合适。你试过调试吗?调用代码对吗?我看到它将 cl 清零 - 你确定调用代码不是在 cl 中寻找返回值吗?
  • 尚未调试(作为新手,对 gdb 等不太熟悉),但它是一段非常直接的代码。期望它的外壳取决于进位 flah(因此 stcclc)。但它总是返回不相等,所以它甚至不是放错位置的问题 clcstc
  • 作为您上一个问题中完整代码的一部分,这对我来说很好。
  • 我认为xor 会改变你的旗帜!
  • 你说得对@Jester,我在中间添加了一个异或,但我最初从未运行过旧版本。

标签: assembly operating-system x86 nasm


【解决方案1】:

设置进位标志后,您正在异或 cl。这是个坏主意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多