汇编计数器代码将永远无法工作或循环

时间:2018-10-12 05:48:32

标签: assembly while-loop x86 nasm

所以,我的目标是使循环通过x运行并打印msgTrue直到计数器等于零。从理论上讲,这应该起作用。我可能只是弄乱了寄存器。

comparesCounter:

    cmp ah, 0     ;ah stores the amount of repetitions I want the code to go through
    jne notNull   ;jump if not true  
    jmp exit

notNull:      
    dec ah             ;ah -- 
    mov eax, 4         ;|
    mov ebx, 1         ;|
    mov ecx, msgTrue   ;|>this code prints out what's stored in msgTrue
    mov edx, len1      ;|
    int 80h            ;|

    jmp comparesCounter ;jumps up into counter

我应该使用其他寄存器吗?或者仅仅是出于愚蠢程度我的代码概念无济于事?

1 个答案:

答案 0 :(得分:0)

问题在于修改ah也会修改ah。这是一个简单的图,显示了eax eax -------------------------- | | ax | | | ----------- | | | | ah | al | | | | ----------- | --------------------------- 3 2 1 0 之间的关系:

ah

如您所见,axeax的最高有效部分,而这又是eax = 4的最低有效的一半。因此,当您设置ah = 0时,就是在隐式设置ah

如果您想继续使用push eax ; Save eax's current value on the stack mov eax, 4 ... int 80h pop eax ; Restore eax from the stack 作为循环计数器,则可以将其临时放在堆栈上:

Name  |   ID |  Total |  CityName
--------------------------------
A         1       2        ABC
--------------------------------
B         2       1        XYZ
--------------------------------
C         3       1        ABC
--------------------------------
相关问题