在Al寄存器中显示值

问题描述 投票:0回答:1

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

这是我使用的代码:

; 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
assembly dos x86-16 emu8086
1个回答
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转换后显示它们。

© www.soinside.com 2019 - 2024. All rights reserved.