Assembly Language x86 - Registers Set and Arithmetic and Loop

Viewed 86

I am trying to solve this problem about loops. I am using a push and pop method instead of using a separate register to store data.

.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

I am unable to loop the mov al, '1'.

The output should be like this:

A1
B2
C3
D4
E5

Can anyone show the correct code? Thank you.

1 Answers

Consider the ASCII codes involved:

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

See how the difference is always 16 ? That's what next solution exploits:

    ...
    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
    ...

You don't always need to use CX and the LOOP instruction to work with a loop. Anyway the LOOP instruction is to be avoided for speed reasons!

Related