Search code examples
assemblyx86-16emu8086

INT 10H function 06H moves the cursor


I run the following assembly code on (emu8086):

clearScr macro
         mov ah, 06h
         mov al, 00h
         mov bh, 71h
         mov cx, 0000h
         mov dx, 184fh
         int 10h
clearScr endm

print macro string
    mov ah, 09h    
    lea dx, string
    int 21h        
print endm

.model small
.stack 100h

.data
    msg    db 'Hello, world!', '$'  
    nl     db 0AH, 0DH, '$' 
    myname db 'Ahmed', '$'

.code                   
    main    proc
        mov ax, @data
        mov ds, ax

        print myname
        clearScr
        print msg

        mov ax, 4c00h
        int 21h
    main    endp

and the clearScr macro seems to be moving the cursor as the final text printed on the screen isn't starting from the corner.figure 0

Why is this happening? I'm not trying to set/move the cursor.


Solution

  • and the clearScr macro seems to be moving the cursor

    It is exactly the opposite:

    clearScr is not moving the cursor but you want it to be moved.

    What happens is the following:

    • print myname will move the cursor; the cursor will no longer be located in the top left corner but it will be located at the end of your name.
    • If I understood correctly, clearScr will clear the screen but not change the cursor position; this means that the cursor will remain in the position where it was after print myname
    • print msg will also not change the cursor position before printing the message, so the message will be printed to the position where the cursor was after print myname

    If you want to clear the screen and to move the cursor to the corner, you will have to do two steps:

    • Clear the screen (this will not move the cursor to the corner)
    • Move the cursor to the corner (for example with int 10h function AH=2)

    I see you print Ahmed, but you don't do a cr/lf afterwords.

    If I understand correctly, printing CR/LF would cause the text to be printed in the second line after clearing the screen.

    Printing CR only would move the cursor to the top left corner if the cursor is in already the top line.