كود:
[FONT='Tahoma','sans-serif'];This program prompts to enter stream character ended with CR. Calculate cumulative sum Print Sum in Hex.
.MODEL SMALL
.STACK 100H
.DATA
MSG DB 0Dh,0Ah,'Enter a [/FONT]
[FONT='Tahoma','sans-serif']hex # (0...FFFF): $'
MSG1 DB 0Dh,0Ah,'The Cumulative Sum in Hex is:$ '
SUM DW 0 [/FONT]
[FONT='Tahoma','sans-serif'][/FONT]
[FONT='Tahoma','sans-serif'].CODE
main proc
mov ax,@data
mov ds,ax
;display a message
mov cl,4 ;initialise # if shifts
top: mov ah,9
LEA dx,MSG
int 21h
XOR BX,BX ; initilalise number
XOR Dx,DX ; initialise sum of numbers
;read a character
mov ah,1
While_: int 21h
;check for end of string input, if not,add to [/FONT]
[FONT='Tahoma','sans-serif']sum
CMP al,0DH
JE End_while
cmp al,'9'
JG alpha
sub al,30h
jmp insert
alpha: sub al,37h
insert: mov dl,al
add SUM, DX ; to add a read value to sum
shl bx,cl
OR bl,al
[/FONT]
[FONT='Tahoma','sans-serif']Jmp while_ ;loop back to read next character
; display Sum stored in BX as hex output
End_while:
getsum:
mov BX,SUM
mov AH,9
lea DX,MSG1
INT 21H
MOV AH,2
mov dl,Bh
noc: mov ch,4 ;repeat 4 times to display 4 hex digits in SUM
Mov [/FONT]
[FONT='Tahoma','sans-serif']cl,4 ; number of shifts
next: Mov dl,bh ;start printing from the left,print digit in high byte
SHR dl,cl ;shift to get the first one digit only from the hi byte
CMP dl,09h ;check SUM stored as hex letter(A..F)/or hex#(0..9)
Jg letter
ADD [/FONT]
[FONT='Tahoma','sans-serif']dl,30h ;convert to ascii character of decimal code
Jmp display
letter: Add dl,37h ;convert to ascii character of letter code
display: int 21h ;print it
ROL bx,cl ;get next stored digit from the left
dec ch ;decrement ch and repeat if ch is not zero
jnz next
mov [/FONT]
[FONT='Tahoma','sans-serif']ah,4Ch
int 21h
main endp
end main [/FONT]
[FONT='Tahoma','sans-serif'][/FONT]