1: TITLE Addition of two integers in ASCII form ASCIIADD.
ASM
2: COMMENT |
3: Objective: To demonstrate addition of two integers
4: in the ASCII representation.
5: Input: None.
6: | Output: Displays the sum.
7: .MODEL SMALL
8: .STACK 100H
9: .DATA
10: sum_msg DB 'The sum is: ',0
11: number1 DB '1234567890'
12: number2 DB '1098765432'
13: sum DB 10 DUP (' '),0 ; add NULL char. to use PutStr
14:
15: .CODE
16: INCLUDE [Link]
17: main PROC
18: .STARTUP
Logical: 1
19: ; SI is used as index into number1, number2, and sum
20: mov SI,9 ; SI points to rightmost digit
21: mov CX,10 ; iteration count (# of digits)
22: clc ; clear carry (we use ADC not ADD)
23: add_loop:
24: mov AL,number1[SI]
25: adc AL,number2[SI]
26: aaa ; ASCII adjust
27: pushf ; save flags because OR
28: or AL,30H ; changes CF that we need
29: popf ; in the next iteration
30: mov sum[SI],AL ; store the sum byte
31: dec SI ; update SI
32: loop add_loop
33: PutStr sum_msg ; display sum
34: PutStr sum
35: .EXIT
36: main ENDP
37: END main
Logical: 2
1: TITLE Addition of integers in packed BCD form [Link]
2: COMMENT |
3: Objective: To demonstrate addition of two integers
4: in the packed BCD representation.
5: Input: None.
6: | Output: Displays the sum.
7: SUM_LENGTH EQU 10
8: .MODEL SMALL
9: .STACK 100H
10: .DATA
11: sum_msg DB 'The sum is: ',0
12: number1 LABEL BYTE
13: DT 1234567890 ; stores in packed BCD form
14: number2 LABEL BYTE
15: DT 1098765432 ; stores in packed BCD form
16: BCDsum LABEL BYTE
17: DT ?
18: ASCIIsum DB SUM_LENGTH DUP (' '),0 ; add NULL char.
19:
20: .CODE
21: .486
22: INCLUDE [Link]
23: main PROC
24: .STARTUP
Logical: 3
25: sub SI,SI
26: mov CX,5 ; loop iteration count
27: clc ; clear carry (we use ADC)
28: add_loop:
29: mov AL,number1[SI]
30: adc AL,number2[SI]
31: daa ; ASCII adjust
32: mov BCDsum[SI],AL ; store the sum byte
33: inc SI ; update index
34: loop add_loop
35: call ASCII_convert
36: PutStr sum_msg ; display sum
37: PutStr ASCIIsum
38: .EXIT
39: main ENDP
40: ;-----------------------------------------------------------
41: ; Converts the packed decimal number (5 digits) in BCDsum
42: ; to ASCII represenation and stores it in ASCIIsum.
43: ; All registers are preserved.
44: ;-----------------------------------------------------------
45: ASCII_convert PROC
46: pusha ; save registers
Logical: 4
47: ; SI is used as index into ASCIIsum
48: mov SI,SUM_LENGTH-1
49: ; DI is used as index into BCDsum
50: sub DI,DI
51: mov CX,5 ; loop count (# of BCD digits)
52: cnv_loop:
53: mov AL,BCDsum[DI] ; AL := BCD digit
54: mov AH,AL ; save the BCD digit
55: ; convert right digit to ASCII & store in ASCIIsum
56: and AL,0FH
57: or AL,30H
58: mov ASCIIsum[SI],AL
59: dec SI
60: mov AL,AH ; restore the BCD digit
61: ; convert left digit to ASCII & store in ASCIIsum
62: shr AL,4 ; right shift by 4 positions
63: or AL,30H
64: mov ASCIIsum[SI],AL
65: dec SI
66: inc DI ; update DI
67: loop cnv_loop
68: popa ; restore registers
69: ret
70: ASCII_convert ENDP
71: END main
Logical: 5