首页 > 解决方案 > 汇编语言 x86 - 寄存器集、算术和循环

问题描述

我正在尝试解决有关循环的问题。我使用的是推送和弹出方法,而不是使用单独的寄存器来存储数据。

.model small
.stack
.code
m   proc
    mov ax,0b800h
    mov es,ax
    mov di,7d0h
    mov ah,7          ; normal attribute
    mov al,'A'
    mov cx,5
x:  stosw
    push ax           ;mov dl,al ; dl='A'
    push di
    mov al,'1'
    stosw
    pop di
    add di,158
    pop ax            ;mov al,dl
    inc al
    loop x
    mov ah,4ch
    int 21h
m   endp
end m

我无法循环播放mov al, '1'.

输出应该是这样的:

A1
B2
C3
D4
E5

任何人都可以显示正确的代码吗?谢谢你。

标签: loopsassemblyx86dosbox

解决方案


考虑所涉及的 ASCII 码:

     Letter Digit Difference
A1   65     49    16
B2   66     50    16
C3   67     51    16
D4   68     52    16
E5   69     53    16

看到差异总是 16 吗?这就是下一个解决方案所利用的:

    ...
    mov  ax, 0700h + 'A'    ; WhiteOnBlack 'A'
x:  stosw                   ; Stores one letter from {A, B, C, D, E}
    sub  al, 'A' - '1'      ; Convert from letter to digit
    stosw                   ; Stores one digit from {1, 2, 3, 4, 5}
    add  al, 'A' - '1' + 1  ; Restore AL and at the same time increment
    add  di, 160 - 4        ; Move down on the screen
    cmp  al, 'E'
    jbe  x
    ...

您并不总是需要使用CXLOOP指令来处理循环。无论如何,LOOP出于速度原因,应避免使用该指令!


推荐阅读