.
model small
.stack 100h
.data
msg1 db 'Enter first 2-digit BCD number: $'
msg2 db 13,10,'Enter second 2-digit BCD number: $'
msgSum db 13,10,'Sum: $'
numStr1 db 2 dup(?) ; to hold 2 digits
numStr2 db 2 dup(?)
num1 db ?
num2 db ?
result db ?
.code
start:
mov ax, @data
mov ds, ax
; Prompt for first number
lea dx, msg1
mov ah, 09h
int 21h
; Read two ASCII digits into numStr1
lea si, numStr1
call ReadTwoDigits
; Convert to BCD
lea si, numStr1
call ConvertAsciiToBcd
mov num1, al
; Prompt for second number
lea dx, msg2
mov ah, 09h
int 21h
; Read two ASCII digits into numStr2
lea si, numStr2
call ReadTwoDigits
; Convert to BCD
lea si, numStr2
call ConvertAsciiToBcd
mov num2, al
; BCD Addition
mov al, num1
add al, num2
daa ; Decimal adjust for BCD result
mov result, al
; Print Sum Message
lea dx, msgSum
mov ah, 09h
int 21h
; Display result as 2 ASCII digits
mov al, result
mov ah, al
and ah, 0F0h
shr ah, 4
add ah, '0'
mov dl, ah
mov ah, 02h
int 21h
mov al, result
and al, 0Fh
add al, '0'
mov dl, al
mov ah, 02h
int 21h
; Exit program
mov ah, 4Ch
int 21h
; -------------------------------
; ReadTwoDigits: reads 2 chars into [SI] and [SI+1]
; -------------------------------
ReadTwoDigits:
mov cx, 2
ReadLoop:
mov ah, 01h ; Read character
int 21h
mov [si], al
inc si
loop ReadLoop
ret
; -------------------------------
; ConvertAsciiToBcd: Converts two ASCII digits at [SI] into packed BCD in AL
; Example: '2','7' → 0x27
; -------------------------------
ConvertAsciiToBcd:
mov al, [si] ; Get first digit
sub al, '0'
shl al, 4 ; Shift into high nibble
mov bl, al
mov al, [si+1] ; Get second digit
sub al, '0'
or al, bl ; Combine
ret
end start