tom younh
tom younh

Reputation: 11

Assembly Language: Teaching myself, run into a bit of trouble

I have seem to run into a problem. I have taught myself to print all characters of the ascii table with this code:

.MODEL SMALL
 .STACK 100H

 .DATA
    PROMPT  DB  'The 256 ASCII Characters are : $'

 .CODE
   MAIN PROC
     MOV AX, @DATA                ; initialize DS 
     MOV DS, AX

     LEA DX, PROMPT               ; load and print PROMPT 
     MOV AH, 9
     INT 21H

     MOV CX, 256                  ; initialize CX

     MOV AH, 2                    ; set output function  
     MOV DL, 0                    ; initialize DL with first ASCII character

     @LOOP:                       ; loop label
       INT 21H                    ; print ASCII character

       INC DL                     ; increment DL to next ASCII character
       DEC CX                     ; decrement CX
     JNZ @LOOP                    ; jump to label @LOOP if CX is 0

     MOV AH, 4CH                  ; return control to DOS
     INT 21H
   MAIN ENDP
 END MAIN

However, I am in a little bit over my head with how to print it backwards after going forwards and then print every fifth ASCII character. Print the 0 ASCII character followed by a comma then the 255 ASCii Character Ouput 0 (ASCII), 255(ASCII) 1 (ASCII), 254(ASCII)

If anyone would like to offer advice or add to my code for me I would greatly appreciate it.

Upvotes: 1

Views: 268

Answers (2)

Sep Roland
Sep Roland

Reputation: 39676

The code you wrote does not display all of the 256 ASCII characters. DOS will not display the characters for ASCII codes 7, 8, 9, 10, and 13. DOS will perform Beep, Backspace, Tab, Linefeed, and Carriage return. You could use BIOS function 0Ah to output any character but you will have to advance the cursor yourself.
I wrote a small adaptation of your program to demonstrate that you didn't have to use the explicite counter in CX because incrementing the DL register already provides adequate flag information. Please note that the DOS Terminate function 4Ch also uses AL as a parameter.

.MODEL SMALL
.STACK 100H

.DATA
  PROMPT  DB  'The ASCII Characters from 32 to 255 are : $'

.CODE
  MAIN PROC
    MOV AX, @DATA              ; initialize DS 
    MOV DS, AX
    LEA DX, PROMPT             ; load and print PROMPT 
    MOV AH, 9
    INT 21H

    ; Here you can insert my second code snippet

    MOV DL, 32                 ; initialize DL with first ASCII character
    @LOOP:                     ; loop label
    MOV AH, 2                  ; set output function
    INT 21H                    ; print ASCII character
    INC DL                     ; increment DL to next ASCII character
    JNZ @LOOP                  ; jump to label @LOOP if DL is 0

    MOV AX, 4C00H              ; return control to DOS
    INT 21H
  MAIN ENDP
END MAIN

Here is the code to display the ASCII's from 0 to 31.

Again:
 mov cx, 1
 mov bx, 0007h
 mov al, dl
 mov ah, 0Ah
 int 10h      ;WriteCharacter
 push dx
 mov ah, 03h
 int 10h      ;GetCursorPostion
 inc dl       ;Advance column
 mov ah, 02h
 int 10h      ;SetCursorPosition
 pop dx
 inc dl       ;Next character
 cmp dl, 32
 jb Again

Upvotes: 1

I changed your code a lot, I hope it helps you to learn, next little program (made with EMU8086) prints all ASCII characters (your original algorithm), then print them backwards (with some changes in your original algorith), then prints every fifth char, and finally prints them alternate (char 0, char 255, char 1, char 254, etc):

.MODEL SMALL
.STACK 100H

.DATA   

PROMPT  DB  'The 256 ASCII Characters are : $'
PROMPT2 DB  13,10,13,10,'Now backwards : $'
PROMPT3 DB  13,10,13,10,'Now every fifth character : $'
PROMPT4 DB  13,10,13,10,'Now alternated : $'

.CODE
MAIN PROC

 MOV AX, @DATA                ; initialize DS 
 MOV DS, AX

;ONWARDS---------------------------------------------.

 LEA DX, PROMPT               ; load and print PROMPT 
 MOV AH, 9
 INT 21H

 MOV CX, 256                  ; initialize CX

 MOV AH, 2                    ; set output function  
 MOV DL, 0                    ; initialize DL with first ASCII character

 @LOOP:                       ; loop label
   INT 21H                    ; print ASCII character

   INC DL                     ; increment DL to next ASCII character
   DEC CX                     ; decrement CX
 JNZ @LOOP                    ; jump to label @LOOP if CX is 0

;BACKWARDS-----------------------------------------------.

 LEA DX, PROMPT2              ; load and print PROMPT2
 MOV AH, 9
 INT 21H

 MOV CX, 256                  ; initialize CX

 MOV AH, 2                    ; set output function  

 @LOOP2:                       ; loop label
   mov dl, cl ;USE CL AS CHARACTER BECAUSE IT'S GOING BACKWARDS.
   INT 21H                    ; print ASCII character
   DEC CX                     ; decrement CX
 JNZ @LOOP2                    ; jump to label @LOOP if CX is 0

;EVERY FIFTH-----------------------------------------------.

 LEA DX, PROMPT3              ; load and print PROMPT3
 MOV AH, 9
 INT 21H

 MOV CX, 256                  ; initialize CX

 MOV AH, 2                    ; set output function  
 MOV DL, 0                    ; initialize DL with first ASCII character

 @LOOP3:                       ; loop label
   INT 21H                    ; print ASCII character

   add dl, 5 ; increment DL BY 5 to next ASCII character
   sub CX, 5 ; decrement CX BY 5 TOO
   cmp cx, 0 ; IN CASE CX WON'T REACH EXACT ZERO.
   jge @LOOP3                   ; jump to label @LOOP if CX is 0

;ALTERNATED-------------------------------------------------------.

 LEA DX, PROMPT4              ; load and print PROMPT 
 MOV AH, 9
 INT 21H

 MOV CX, 256                  ; initialize CX

 MOV AH, 2                    ; set output function  
 MOV bl, 0                    ; initialize DL with first ASCII character
 MOV bh, 255                  ; initialize DL with last ASCII character

 @LOOP4:
   mov dl, bl  ;FIRST CHARS.
   INT 21H                    ; print ASCII character
   mov dl, bh ;LAST CHARS.
   INT 21H                    ; print ASCII character

   INC bl                     ; increment DL to next ASCII character
   dec bh
   sub CX, 2 ; decrement CX BY 5 TOO
   cmp cx, 0 ; IN CASE CX WON'T REACH EXACT ZERO.
   jge @LOOP4                   ; jump to label @LOOP if CX is 0



;WAIT FOR ANY KEY.    
  mov  ah, 7
  int  21h


 MOV AH, 4CH                  ; return control to DOS
 INT 21H

MAIN ENDP
END MAIN

Upvotes: 2

Related Questions