我做在你的代碼的一些變化,並從另一個答案偷PROC number2string
(充分的意見,幫助您瞭解):
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
任何數字轉換爲字符串中的斧頭和商店變量resultat
中的結果(因爲它在數據段中定義)。
你使用什麼彙編器? MASM,TASM,FASM,NASM ......? – zx485
Ela,你的結果在'ax'中是二進制的,你必須將它轉換爲字符串來顯示它。如果你顯示'ax',你會在屏幕上看到奇怪的字符。 –
am使用emu 8086 –