Search code examples
assemblyx86-16masmtasmcursor-position

when I try to overflow the line size in text mode in assembly 8086 it overflows most of the text in the right place except for the last 3-chars


.Model small

.stack 64

.data

mesg db "What is your name? ",'$'

.code 

Main PROC far 

mov ax,@data

mov ds,ax        


; clear the screen 

mov ax,0600h

mov cx,0

mov dx,184FH

int 10h

; change to text mode 

mov ax,3

int 10h

; place cursor on row=15 col=20

mov ah,2

mov dx,1521

int 10h

; Prompt Mesg into the screen 

mov ah,09

mov dx,offset mesg

int 21h 



Main ENDP

end Main

an image of the error that happened to me


Solution

  • Your code is invoking what C users would call "undefined behaviour". It seems like you accidentally forgot the h in your constant and now you are curious why your program behaves the way it does. As you calculated yourself in the comments, you told the BIOS to put the cursor into column 241. With the video BIOS in your PC (emulator), this works as expected until column 255, that is for 15 characters. When the BIOS increments the cursor column from 255 to 0, it notices the carry and applies an emergency fixup: it injects a newline (as it also would have injected a newline when it increments the cursor column from 79 to 80 in 80 character modes). This makes the cursor jump from row 5, column 256 to row 6, column zero.

    You should be aware that printing a string using the DOS function invokes the console output driver (INT 29) for every single character, and the console output driver forwards each character individually to the BIOS "print TTY character" service (INT 10, AH=0E). Each character printing needs to load the current cursor position, print a single character and update the cursor position. The cursor position is the only state held between the printing the characters. That's why the string is not just printed character by character to the screen position indicated by your invalid cursor position, but it jumps to a completely different (but now validly described) position after printing a couple of characters.