我如何以二进制/十进制格式打印寄存器ds的内容?

时间:2019-10-16 19:55:34

标签: assembly emu8086

我需要一些帮助以二进制和十进制显示寄存器DS的内容,而我所做的只是将十六进制转换为二进制。如何显示我的需求?

.MODEL SMALL
 .STACK 100H

  .DATA
   PROMPT_1  DB  0DH,0AH,'Enter the character : $'
   PROMPT_2  DB  0DH,0AH,'The ASCII code of the given number in HEX       form     is : $'
   PROMPT_3  DB  0DH,0AH,'The ASCII code of the given number in BIN       form     is : $'
   MY_CHAR   DB  ?          
   BINARY    DB  9 DUP('$') 

 .CODE
  MAIN PROC

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

 START:                      ; jump label 
  ; LEA DX, PROMPT_1           ; load and display the string PROMPT_1
   ;MOV AH, 9
   ;INT 21H

   MOV AH, 1                  ; read a character
   INT 21H                                      

   MOV MY_CHAR, AL ; ?¦ save char to use in binary conversion.
   MOV BL, AL                 ; move AL to BL

   CMP BL, 0DH                ; compare BL with CR
   JE END                    ; jump to label @END if BL=CR

   LEA DX, PROMPT_2           ; load and display the string PROMPT_2
   MOV AH, 9
   INT 21H

   XOR DX, DX                 ; clear DX
   MOV CX, 4                  ; move 4 to CX

   LOOP_1:                   ; loop label
     SHL BL, 1                ; shift BL towards left by 1 position
     RCL DL, 1                ; rotate DL towards left by 1 position
                              ; through carry
   LOOP LOOP_1               ; jump to label @LOOP_1 if CX!=0

   MOV CX, 4                  ; move 4 to CX

   LOOP_2:                   ; loop label
     SHL BL, 1                ; shift BL towards left by 1 position
     RCL DH, 1                ; rotate DH towards left by 1 position
                              ; through carry
   LOOP LOOP_2               ; jump to label @LOOP_2 if CX!=0

   MOV BX, DX                 ; move DX to BX
   MOV CX, 2                  ; initialize loop counter

   LOOP_3:                   ; loop label
     CMP CX, 1                ; compare CX wiht 1
     JE SECOND_DIGIT         ; jump to label @SECOND_DIGIT if CX=1
     MOV DL, BL               ; move BL to DL
     JMP NEXT                ; jump to label @NEXT

     SECOND_DIGIT:           ; jump label
       MOV DL, BH             ; move BH to DL

     NEXT:                   ; jump label

     MOV AH, 2                ; set output function

     CMP DL, 9                ; compare DL with 9
     JBE NUMERIC_DIGIT       ; jump to label @NUMERIC_DIGIT if DL<=9
     SUB DL, 9                ; convert it to number i.e. 1,2,3,4,5,6
     OR DL, 40H               ; convert number to letter i.e. A,B...F
     JMP DISPLAY             ; jump to label @DISPLAY

     NUMERIC_DIGIT:          ; jump label
       OR DL, 30H             ; convert decimal to ascii code

     DISPLAY:                ; jump label
       INT 21H                ; print the character
   LOOP LOOP_3               ; jump to label @LOOP_3 if CX!=0


;? FROM CHAR TO BINARY ?
     LEA SI, BINARY+7 ; ?¦ point to string in data segment.
     MOV CX, 8        ; ?¦ maximum number of binary digits.
   BIN_CONVERSION:
     SHR MY_CHAR,1    ; ?¦ get rightmost bit.
     JC  BIT1
     MOV [BYTE PTR SI], '0'
     JMP BIN_SKIP
   BIT1:
     MOV [BYTE PTR SI], '1'
   BIN_SKIP:
     DEC SI
     LOOP BIN_CONVERSION

     LEA DX, PROMPT_3 ; ?¦ display message.
     MOV AH, 9
     INT 21H         
     LEA DX, BINARY   ; ?¦ display binary.
     MOV AH, 9
     INT 21H         

   JMP START                 ; jump to label @START

 END:                        ; jump label

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

1 个答案:

答案 0 :(得分:1)

  

我需要一些帮助以二进制和十进制显示寄存器DS的内容

只需将DS移至AX并使用下面显示的转换例程。

mov ax, ds
  

,我怎么可能显示给定地址的内容(例如010十六进制)?

如果该内存包含一个字节(8位),请写:

mov al, [0010h]
mov ah, 0

如果该内存包含一个字(16位),请写:

mov ax, [0010h]

然后使用下一个例程转换为十进制表示形式

  mov     bx, 10
  xor     cx, cx     ;Counts the number of digits
again:
  xor     dx, dx
  div     bx
  push    dx
  inc     cx
  test    ax, ax
  jnz     again
more:
  pop     dx
  add     dl, 48    ;Convert to character
  mov     ah, 02h
  int     21h
  loop    more

有关其工作原理的详细说明,请参见Displaying numbers with DOS