使用汇编器8086显示表的总和 [英] Displaying the sum of a table using assembler 8086

查看:101
本文介绍了使用汇编器8086显示表的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,使我可以汇总一个表,然后使用汇编语言显示其结果.到目前为止,这是我要提出的:

I am writing some code that allows me to sum a table and then display its result using assembler language. Here is what I've come up with so far:

data segment 
  tab      db 9 dup(3 5 8 4 7 1 6 7 0)
  resultat db ?
data ends

code segment 
  xor di, di
  mov cx, 9
Prog: 
  mov al, tab[di]
  add ax, al
  inc di
loop prog
  mov resultat ,ax
  mov ah, 09h
  int 21h
end code

推荐答案

我对您的代码进行了一些更改,并从另一个答案中窃取了proc number2string(充满注释以帮助您理解):

I made some changes in your code and stole proc number2string from another answer (full of comments to help you understand) :

data segment 
tab db 3,5,8,4,7,1,6,7,0 ;ARRAY OF NUMBERS.
resultat db '     $'  ;STRING WITH 5 CHARS.
data ends
code segment 
mov ax,@data    ;INITIALIZE
mov ds,ax       ;DATA SEGMENT.

xor di,di 
mov cx,9
xor ax,ax       ;CLEAR THE SUM ACCUMULATOR.
Prog:
add al,tab[di]
inc di
loop prog     

call number2string ;CONVERT AX TO STRING IN RESULTANT.

lea dx,resultat
mov ah,09h
int 21h

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

;------------------------------------------
;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.
;THE STRING IS STORED IN VARIABLE "RESULTAT".

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.
  lea  si, resultat
cycle2:  
  pop  dx        
  add  dl, 48 ;CONVERT DIGIT TO CHARACTER.
  mov  [si], dl
  inc  si
  loop cycle2  

  ret
endp  
;------------------------------------------

end code

proc number2string将ax中的任何数字转换为字符串,并将结果存储在变量resultat中(如在数据段中所定义).

The proc number2string converts to string any number in ax and stores the result in variable resultat (as it is defined in the data segment).

这篇关于使用汇编器8086显示表的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆