在Al寄存器中显示值

时间:2017-12-21 21:06:05

标签: assembly dos x86-16 emu8086

我想在Al寄存器中显示值,在我的情况下是0Ah,这里是我的代码但是没有发生,我不确定,但我认为我的问题是我有一个六进制数字注册,但我只能打印字符串或字符串,所以有没有办法将六进制数转换为字符串,例如

这是我使用的代码:

; You may customize this and other start-up templates; 
; The location of this template is c:\emu8086\inc\0_com_template.txt

org 100h


.data segment

Array DB 0h,0h,0h,0h,0h 
x db 0h  
result db  ?

.code segment 

mov si,0 

loop1:
       ;these 5 lines allows me to take
       ;input and let it be shown on the screen

      mov ah,08h
      int 21h
      mov ah,02h
      mov dl,al
      int 21h

      ;those 3 lines allows me
      ;to start my code when
      ;enter button is pressed

      mov bl,0Dh
      cmp bl,dl
      JZ start

      ;those 4 lines allow me
      ;to enter 4 chars and
      ;fill an array with them
      ;to use them later

      mov array[si],dl
      inc si
      cmp si,5
      jne loop1 


start:

      ;putting each element in the
      ;array in a register to be 
      ;able to deal with it

      mov si,0
      mov al,array[si]
      mov bl,array[si+1]
      mov cl,array[si+2]
      mov dl,array[si+3] 

      ;subtracting 30h from each 
      ;number to turn it to its
      ;decimal form to deal with
      ;it as normal number

      sub al,30h
      sub bl,30h
      sub cl,30h
      sub dl,30h 

      ;adding all numbers in
      ;variable called result

      add al,cl
      add al,bl
      add al,dl

      ;printing

      mov ah,02h
      mov dl,al
      int 21h
      ret

1 个答案:

答案 0 :(得分:0)

首先,您忘记在计算中包含数组的最后一个元素。

要在计算中包含数组的最后一个元素的代码:

添加此行,将元素放在寄存器中:

mov bh, array[si+4]    ; store the last element in BH register

添加此行,将寄存器内容转换为十进制:

sub bh, 30h             ; convert it to decimal  

添加此行,添加所有寄存器的内容:

add al, bh              ; add contents of AL with contents of BL 

以小数形式打印总和的代码:

  mov ah, 0     ; clear AH because DIV instruction takes AX for division
  mov cl, 10    ; store the divisor  in CL
  div cl        ; AX/CL remainder will be in AH & quotient in AL

  mov bx, ax    ; move AX value to BX because later instruction are going to overwrite value of AH 

  mov ah,02h     ; print quoteint
  mov dl,bl      
  add dl,30h     
  int 21h  

  mov ah,02h     ; print remainder 
  mov dl,bh
  add dl,30h
  int 21h

因为在你的情况下,添加所有5个元素后的最高数字将是2位数字(9 + 9 + 9 + 9 + 9 = 45),我们只需要划分一次,首先打印商,然后打印余数。如果总和包括超过2位数(如123),那么你必须连续重复相同的过程并将余数存储在堆栈中,直到你得零作为商。然后你可以从堆栈中弹出数字&在进行必要的ASCII转换后显示它们。