Home > Blockchain >  (Assembly Language) I'm trying to get a string to move across the screen 5 times before stoppin
(Assembly Language) I'm trying to get a string to move across the screen 5 times before stoppin

Time:07-11

As the title states, I'm using assembly language to try to get a text to move left to right 5 times. I've pasted my code below. As of right now, what happens is it moves left to right 4 and a half times, starts over at the very left and it repeats infinity times. I feel like the error lies on where I placed my pop cx. I hope that maybe someone else can see the mistake and I just missed it.

.model small
.stack
.data
    strg    db  'Test$'
    row db  12
    col db  0
.code
main    proc
    mov ax,@data
    mov ds,ax

dem:    mov cx,5
    push cx

    mov cx,79   

again:  push cx     ;clears the terminal
    mov ah,6
    mov al,0
    mov bh,7
    mov ch,0
    mov cl,0
    mov dh,24
    mov dl,79
    int 10h

    mov ah,2
    mov bh,0
    mov dh,row  ;row
    mov dl,col  ;col
    int 10h

    mov ah,9
    mov dx, offset strg ;prints the text
    int 21h
    
    inc col

    mov cx,1        ;x and y adds delay
y:  push cx
    
    mov cx,0ffffh
x:  loop x
    pop cx
    
    loop y

    pop cx
    loop again
    pop cx
    loop dem

    mov ah,4ch
    int 21h

main    endp
end main

CodePudding user response:

what happens is it moves left to right 4 and a half times, starts over at the very left and it repeats infinity times.

I don't see where you get that '4 and a half times'. The infinite behaviour starts right away.

dem:    mov cx,5
    push cx

Your outer loop should initialize its counter outside of the loop.

        mov cx,5
dem:    push cx

I feel like the error lies on where I placed my pop cx.

Because your program uses the CX register for a lot of things, it is easy to loose track. Consider using a system that numbers what you push/pop on the stack:

    mov cx,5
dem:
    push cx        ; (1) Outer loop
    mov  cx,79
again:
    push cx        ; (2) Inner loop

    mov ah,6
    mov al,0
    mov bh,7
    mov ch,0
    mov cl,0
    mov dh,24
    mov dl,79
    int 10h

    mov ah,2
    mov bh,0
    mov dh,row
    mov dl,col
    int 10h

    mov ah,9
    mov dx, offset strg
    int 21h
    
    inc col

    mov  cx,1
y:  push cx        ; (3) Waiting loop
    mov  cx,0ffffh
x:  loop x
    pop  cx        ; (3)
    loop y

    pop  cx        ; (2)
    loop again
    pop  cx        ; (1)
    loop dem

You don't always need to use CX. There are more registers at your disposal:

    mov  bp,1
y:  mov  cx,0ffffh
x:  loop x
    dec  bp
    jnz  y
  • Related