assembly 如何在emu8086中打印寄存器的内容?

nfeuvbwi  于 2023-03-30  发布在  其他
关注(0)|答案(3)|浏览(179)
include 'emu8086.inc'
#make_com#

org 100h

s1 dw 50,60,70,80,90,100,120,130,140,160,170,190,190,220,250,270,300

    MOV SI,0
    MOV CX,16

s2:
    MOV AX,s1[SI]

s3:
    INC SI
    CMP AX,100
    JBE s4
    JA s5

s4:
    PRINTf AX
    JMP s3

s5:
    CMP AX,200
    JB s6
    JA s7

s6:
    PRINTf AX
    JMP s3

s7:
    PRINTf AX
    JMP s3

    END
    ;printf AX doesn't work and i want to print the contents of AX
oprakyz7

oprakyz71#

;printf AX不工作,我想打印AX的内容
你还没有向我们展示 PRINTf 应该做什么。所以你所有的PRINTf AX宏调用都可以。
但是你的程序确实有两个错误,阻止它正确执行。
1.一个.COM程序从顶部开始执行,但那是你放置数据的地方。这是不能执行的!所以要么把数据放在代码下面,要么跳过数据。
1.您使用的数据是字长的,因此在迭代数据时需要向SI寄存器加2。

xdyibdwo

xdyibdwo2#

您可以按原样显示AX,但您会在屏幕上看到奇怪的字符。将二进制(AX)转换为字符串的过程是必要的。下一个代码将数字放入AX,将AX转换为字符串,并显示字符串。您可以在未来的程序中使用过程number 2string。在EMU 8086中复制粘贴下一个代码并运行它:

.stack 100h
;------------------------------------------
.data
str db 6 dup('$') ;STRING TO STORE NUMBER. 
;------------------------------------------
.code          
;INITIALIZE DATA SEGMENT.
  mov  ax, @data
  mov  ds, ax

;CONVERT NUMBER TO STRING.
  mov  ax, 10382      ;ANY NUMBER.
  call number2string  ;CONVERT AX. RESULT IN "STR".

;DISPLAY STRING.
  mov  ah, 9
  mov  dx, offset str  ;NUMBER CONVERTED.
  int  21h

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

;FINISH PROGRAM.
  mov  ax, 4c00h
  int  21h           

;------------------------------------------

;NUMBER TO CONVERT MUST ENTER IN AX.
;ALGORITHM : EXTRACT DIGITS ONE BY ONE, STORE
;THEM IN STACK, THEN EXTRACT THEM IN REVERSE
;ORDER TO CONSTRUCT STRING.

proc number2string
  mov  bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.
  mov  cx, 0 ;COUNTER FOR EXTRACTED DIGITS.
cycle1:       
  mov  dx, 0 ;NECESSARY TO DIVIDE BY BX.
  div  bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER.
  push dx ;PRESERVE DIGIT EXTRACTED FOR LATER.
  inc  cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.
  cmp  ax, 0  ;IF NUMBER IS
  jne  cycle1 ;NOT ZERO, LOOP. 
;NOW RETRIEVE PUSHED DIGITS.
  mov  si, offset str
cycle2:  
  pop  dx        
  add  dl, 48 ;CONVERT DIGIT TO CHARACTER.
  mov  [ si ], dl
  inc  si
  loop cycle2  

  ret
endp

再解释一下:对于您将来的程序,过程number 2string需要名为“str”的数据段中的变量。正如您在代码中看到的,“str”的长度为6,因为AX可以容纳5位或更少的数字,并且,如果您想要显示它,字符串需要'$'符号,这就是长度为6的原因。number 2string的参数必须放置在AX上。当然,您可以更改变量名(str)和使用的寄存器(AX)。

twh00eeo

twh00eeo3#

这将打印所有寄存器到屏幕上。希望这是你正在寻找的。你可以保存为宏或全局,并随时调用它打印所有寄存器。

org 100h
    jmp start
    
        vec1    db ?   
        alreg   db "AL = $" 
        ahreg   db "AH = $"
        blreg   db "BL = $"
        bhreg   db "BH = $"
        clreg   db "CL = $"
        chreg   db "CH = $"
        dlreg   db "DL = $"
        dhreg   db "DH = $"   
    start:   ;--------------first we do al register------
            mov cx, 8
            mov vec1, al 
            
            lea dx, alreg
            mov ah, 09h
            int 21h
            
            call f1        
       
            ;----now doing ah------- 2
            
            mov vec1, ah 
            
            lea dx, ahreg
            mov ah, 09h
            int 21h
            
            call f1 
            
            ;---now doing bl---------3
            
            
            mov vec1, bl 
            
            lea dx, blreg
            mov ah, 09h
            int 21h
            
            call f1
            
            
            ;---now doing bh---------4
            
            mov vec1, bh
            
            lea dx, bhreg
            mov ah, 09h
            int 21h
            
            call f1
            
            ;---now doing cl---------5
            
            mov vec1, cl
            
            lea dx, clreg
            mov ah, 09h
            int 21h
            
            call f1
            
            ;---now doing ch---------6
            
            mov vec1, ch
            
            lea dx, chreg
            mov ah, 09h
            int 21h
            
            call f1
            
            ;---now doing dl---------7
            
            mov vec1, dl
            
            lea dx, dlreg
            mov ah, 09h
            int 21h
            
            call f1
            
            
            ;---now doing dh---------8
            
            mov vec1, dh
            
            lea dx, dhreg
            mov ah, 09h
            int 21h
            
            call f1     
            call end:
            ;--------------------------done-----
            
             f1 proc
            lea si, vec1 ;load vec1 address to si
            mov al, [si] ;mov content of mem addr pointed by si
            push cx      ;save counter
            mov cx, 2    ;set new counter to work on 2 charcters
                        
            leftnibble:  ;start with left nibble first
            shr al, 4    ;shif right 4x to mov left nibble to right
            push ax      ;save ax before int 21h
            
            mov bl, 0ah  ;mov 10 dec in bl to cmp with al
            cmp al, bl   ;three jmp operations based on zero and carry flag to
                         ;follow. 
            jz printchar  ;jump if zero flag set to 1
            jc printdigit ;jump if carry flag set to 1. des<source.
            jnc printchar ;jmp no carry = des>source           
                          ;right nibble convert operation:
            rightnibble:      
            mov ax, 0     ;zero ax incase leftovers
            mov al, [si]  ;mov content of mem addr pointed by si
            pop bx        ;retrun saved char 
            shl bx, 4     ;shit left 4x moving right nibble to lef.
    
            xor ax, bx    ;clear left nibble to zero           
            mov bl, 0ah   ;set 0ah (10 decimal) to bl to cmp
            cmp al, bl
            jz printchar  ;jump if zero flag set to 1
            jc printdigit ;jump if carry flag set to 1. des<source.
            jnc printchar ;jmp no carry = des>source             
                     
            printdigit:       ;print if any of the charcters is 9 or less
           
            mov dl, al    
            add dl, 30h   ;add 30 hex to convert to ascii
            mov ah, 06h   ; echo to monitor service
            int 21h
                    
            dec cx        ;dec counter 
            jcxz tab      ; jmp if cx is zero
            jmp rightnibble ;go process right side nibble
                    
            printchar:        ;same as printdigit only for letter A-F
            mov dl, al
            add dl, 37h   ;convert A,B,C,D,E or F to ascii
            mov ah, 06h   ;print character function 06h
            int 21h
            
            dec cx         
            jcxz tab
            jmp rightnibble  
    
            tab:
            mov dl, 32    ;ascii #32 is tab
            mov ah, 06
            int 21h
            int 21h       ;inserting 2 tab spaces
            pop cx        ;return counter set initially to 8 at start.
            dec cx
            jcxz end        
            ret
            f1 endp
            
    ;---------------------------   
            
    end:
        mov ax, 4c00h
        int 21h

相关问题