汇编语言:自学,遇到一点麻烦

时间:2015-05-08 23:21:34

标签: assembly x86 dos

我似乎遇到了一个问题。我自学了用这段代码打印ascii表的所有字符:

.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

然而,我有点过头了,如何在前进后向后打印,然后打印每五个ASCII字符。 打印0 ASCII字符,后跟逗号,然后打印255 ASCii字符 输出继电器 0(ASCII),255(ASCII) 1(ASCII),254(ASCII)

如果有人愿意为我提供建议或添加我的代码,我将非常感激。

2 个答案:

答案 0 :(得分:2)

我改变了你的代码很多,我希望它可以帮助你学习,下一个小程序(用EMU8086制作)打印所有ASCII字符(你的原始算法),然后向后打印它们(你的原始算法有一些变化),然后打印每五个字符,最后打印替换它们(char 0,char 255,char 1,char 254等):

.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

答案 1 :(得分:1)

您编写的代码不会显示所有256个ASCII字符。 DOS不会显示ASCII代码7,8,9,10和13的字符.DOS将执行Beep,Backspace,Tab,Linefeed和Carriage返回。您可以使用BIOS功能0Ah输出任何字符,但您必须自己前进光标 我写了一个小程序改编你的程序来证明你没有必要在CX中使用explicite计数器,因为增加DL寄存器已经提供了足够的标志信息。请注意,DOS终止功能4Ch也使用AL作为参数。

.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

以下是显示0到31之间ASCII码的代码。

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