Moein Hosseini
Moein Hosseini

Reputation: 4373

Newline in 8086 assembly language: my text prints stair-stepped

I'm getting stair-step output like this enter image description here

My program works correctly, except that when I print a new line, and then print something in the current line, it shows on the next line but with some space before it.


The program is this: (print a table of multiple numbers from 0 to 9):

data_seg segment
    I DB 0D
    J DB 0D
    R DB ?
    DIVER DB 10D
    data_seg ends

stack_seg segment
    stack_seg ends

code_seg segment
    MAIN proc far
        assume cs:code_seg, ds:data_seg, ss:stack_seg
        MOV AX,data_seg
        MOV DS,AX

        FOR1:
            MOV J,0D
            FOR2:
            MOV AX,0H
            MOV AL,I
            MUL J
            DIV DIVER 
            MOV R,AH
            ADD AL,48D
            MOV AH,0EH
            INT 10H
            MOV AL,R
            ADD AX,48D
            MOV AH,0EH
            INT 10H

            MOV AX,32D
            MOV AH,0EH
            INT 10H
            INC J 
            MOV AX,0
            MOV AL,J
            SUB AX,10D
            JNZ FOR2
         INC I
         MOV AX,10D
         MOV AH,0EH
         INT 10H
         MOV AX,0
         MOV AL,I
         SUB AX,10D
         JNZ FOR1

        MOV AX,4CH
        INT 21H
        MAIN endp
    code_seg ends
end MAIN

Upvotes: 6

Views: 48920

Answers (8)

Ghassen Jemaî
Ghassen Jemaî

Reputation: 82

I had the very same issue while studying assembly in the class, I could fix it using the cert code which is in my case 0D and the exact same code for carriage return.

Upvotes: 0

Sep Roland
Sep Roland

Reputation: 39676

You got a stair-step output because in the code where you'd like to continue on another line, you only have send the control code 10 to the display:

MOV AX,10D
MOV AH,0EH
INT 10H

On the DOS platform, the New Line is represented by two bytes: the first byte is called Carriage Return and it uses control code 13, and the second byte is called Line Feed and it uses control code 10.

This two-byte sequence mimics the action of the ancient typewriter, where you would pull a lever whenever you wanted to continue on the next line.
This lever performed a double action:

  • first it would Return the Carriage to the left-side of the paper
  • and then (while still maintaining the pull) the roller would turn a bit so as to Feed a new Line.

In BIOS it looks like:

mov  ax, 0E0Dh ; BIOS.Teletype
int  10h
mov  al, 10
int  10h

In DOS it looks like:

mov  dl, 13
mov  ah, 02h  ; DOS.PrintCharacter
int  21h      ; -> AL=13
mov  dl, 10
int  21h      ; -> AL=10

If your program already contains a string that ends in 13, 10, '$' then move these 3 bytes to the following line, label the line suitably, and output the new line like:

mov  dx, crlf ; For MASM-type assemblers write `mov dx, offset crlf`
mov  ah, 09h  ; DOS.PrintString
int  21h      ; -> AL='$'

...

msg1 db 'stackoverflow rocks'
crlf db 13, 10, '$'

ps. Although we normally write 13 followed by 10, the emulator seems to prefer 10 coming before 13. Could well be that it's one of its many bugs and not so much an intended choice of the designer(s).


The highly upvoted and accepted answer states:

You need to print new line and carriage return

It's like saying "To print new line, you need to print new line and carriage return".

'New Line' is the name for the combined action, and 'Carriage Return' plus 'Line Feed' are its constituents.

Even on a platform where the carriage return code (13) is not required, you would say: "To print new line, you need to print the linefeed code (10)".

Upvotes: 1

Reza Miraki
Reza Miraki

Reputation: 1

in emu8086 you can use this function:

newLine PROC FAR
MOV AH, 2
MOV DL, 10
INT 21H
MOV DL, 13
INT 21H
RET
newLine endp

Upvotes: 0

bhaskarc
bhaskarc

Reputation: 9541

This would print a new line:

1) Add in the data segment:

linefeed db 13, 10, "$"

2) And then use this anywhere you need a new line:

; new line
mov ah, 09
mov dx, offset linefeed
int 21h

Upvotes: 5

MRNakh
MRNakh

Reputation: 157

if your using emu80x86 this code should do it

mov dx,13
  mov ah,2
  int 21h  
  mov dx,10
  mov ah,2
  int 21h

Upvotes: 9

That coworker
That coworker

Reputation: 79

AS anthony said, Based on your assembler, you need to do a carriage return and line feed to go to next line and place cursor at the beggining of the line. For MASM you can use Call crlf or print values 0dh and 0ah respectively.

Upvotes: 3

Yappie
Yappie

Reputation: 399

try to put lanes for line return

mov ax, 4c00h ; return to ms-dos
int 21h

Upvotes: -3

Anthony Blake
Anthony Blake

Reputation: 5348

You need to print new line and carriage return.

Upvotes: 10

Related Questions