首页 > 解决方案 > 汇编计数器代码将永远无法工作或循环

问题描述

所以,我的目标是让循环通过 x 次运行并打印 msgTrue 直到计数器等于 0。从理论上讲,这应该有效。不过,我可能只是弄乱了寄存器。

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

我是否应该使用其他寄存器,或者仅仅是我的代码的愚蠢程度超出帮助的概念?

标签: assemblywhile-loopx86nasm

解决方案


问题是修改eax也会修改ah. ah这是一个简单的图表,显示和之间的关系eax

           eax
--------------------------
|           |     ax      |
|           | ----------- |
|           | | ah | al | |
|           | ----------- |
---------------------------
  3      2      1     0

如您所见,ah是 的最重要的一半ax,而后者又是 的最不重要的一半eax。所以当你设置时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

推荐阅读