Moeez Ahmad
Moeez Ahmad

Reputation: 11

Code in assembly printing alphabets twice instead of next line. (emu8086)

My code in assembly emu 8086 displays alphabets in lower case but it displays them twice instead of displaying them in new line a single time each. Ive tried everything from AI etc but could not find the solution.

.model Small
.stack 100h
.data
.code
main proc

mov cx,26

mov dl,'a'


loop1:


mov ah,2
int 21h

mov dh,10
mov ah,2
int 21h

inc dl
loop loop1


mov ah,4ch
int 21h

end main
main endp

Upvotes: -2

Views: 65

Answers (1)

Sep Roland
Sep Roland

Reputation: 39516

It is foremost a typo

mov dh,10 does not load the required DL register for use by the DOS.DisplayCharacter function. It is mandatory to write mov dl, 10, and because on the DOS platform a newline is actually composed of the 2 control codes 13 (carriage return) and 10 (linefeed), you need to send both these codes to the screen.

Correcting the typo is not enough

While loading the DL register with the newline codes (one after the other), you will loose the alphabetical character that you have stored in DL. A good solution could be loading the character from a spare register like BL.

 mov  cx, 26
 mov  bl, 'a'
loop1:
 mov  dl, bl
 mov  ah, 02h
 int  21h
 mov  dl, 13
 mov  ah, 02h
 int  21h
 mov  dl, 10
 mov  ah, 02h
 int  21h
 inc  bl
 loop loop1

A bit of fine tuning

  • not using a separate iteration counter but relying on the final character that we want to display
  • not repeatedly loading the function number in AH and hoisting its one-time loading out of the loop
  • not using more registers than what is strictly needed (here we just clobber AX and DX) which is useful for cases where there's register pressure
 mov  ah, 02h   ; CONST
 mov  dh, 'a'
loop1:
 mov  dl, dh
 int  21h
 mov  dl, 13
 int  21h
 mov  dl, 10
 int  21h
 inc  dh
 cmp  dh, 'z'
 jbe  loop1

jbe dh, 'z' repeats the loop for as long as DH holds a value smaller or equal to 'z'.

Upvotes: 0

Related Questions