簡體   English   中英

在Al寄存器中顯示值

[英]Displaying the value in Al register

我想在Al寄存器中顯示值為0Ah的值,這是我的代碼,但是nothig發生了,我不確定,但是我認為我的問題是我的寄存器中有一個六進制數,但是我只能打印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

首先,您忘記了在計算中包括數組的最后一個元素。

在計算中包括數組最后一個元素的代碼:

將以下行添加到將元素放入寄存器的位置:

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轉換后顯示它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM