Skip to content

Commit

Permalink
feat(threedigit): same as twodigit but show 3 digit if sum > 99
Browse files Browse the repository at this point in the history
  • Loading branch information
mavyfaby committed Mar 12, 2023
1 parent 0778858 commit af0b6da
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Compilation of my DOS x86 Assembly programs
## Programs

- [TWODIGIT](TWODIGIT.ASM) - Add 2 numbers, show 2 digits if sum > 9.
- [THREEDIGIT](THREEDIGIT.ASM) - Same as `TWODIGIT`, but show 3 digits if sum > 99.
- [DIVISION](DIVISION.ASM) - Divide 2 numbers, show quotient with 5-digit precision.

## License
Expand Down
246 changes: 246 additions & 0 deletions THREEDIGIT.ASM
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
jmp start

nm1 db 3, 4 dup(0)
nm2 db 3, 4 dup(0)
quo db 0
rmd db 0
rs1 db 0
rs2 db 0
rsf db 0
fnm db 0
snm db 0

ms1 db 'Enter 1st number: $'
ms2 db 'Enter 2nd number: $'
ms3 db 'Sum: $'
clf db 13,10,'$'

start:

; clear registers
call clear_r
call clear_v
; display message
lea dx,ms1
mov ah,9
int 21h
; get input
lea dx,nm1
mov ah,0ah
int 21h
; print line
call n_line
; display message
lea dx,ms2
mov ah,9
int 21h
; get input
lea dx,nm2
mov ah,0ah
int 21h

; print line
call n_line
; convert inputs to number
call cnv_nm1
call cnv_nm2
; clear registers
call clear_r
; add converted input
mov al,rs1
add al,rs2

mov rsf,al ; store result int result final
; print sum message
lea dx,ms3
mov ah,9
int 21h
; clear registers
call clear_r

; if result is greater than 100
cmp rsf,100
jae call_d3

; if result is greater than 10
cmp rsf,10
jae d2
; if result is less than 10
jmp d1
; stop program
int 20h

cnv_nm1:
mov al, [nm1+2] ; get the first digit
sub al, '0' ; convert to a number
mov fnm, al ; store the first digit
; set result
mov rs1, al
call clear_r

; check if there is a second digit
mov al, [nm1+3]
CMP al, 13
je fun_ret
; if there's a second digit
mov al, [nm1+3] ; get the second digit
sub al, '0' ; convert to a number
mov snm, al ; store the second digit
; multiply first digit by 10
mov al, fnm
mov bl, 10
mul bl
; add second digit to result
add al, snm
mov rs1, al

ret

cnv_nm2:
mov al, [nm2+2] ; get the first digit
sub al, '0' ; convert to a number
mov fnm, al ; store the first digit
; set result
mov rs2, al
call clear_r

; check if there is a second digit
mov al, [nm2+3]
CMP al, 13
je fun_ret
; if there's a second digit
mov al, [nm2+3] ; get the second digit
sub al, '0' ; convert to a number
mov snm, al ; store the second digit
; multiply first digit by 10
mov al, fnm
mov bl, 10
mul bl
; add second digit to result
add al, snm
mov rs2, al

ret

call_d3:
call d3
jmp end

fun_ret:
ret

d1:
mov dl,rsf
or dl,30h
mov ah,2
int 21h

int 20h

d2:
; divide result by 10
mov al,rsf
mov bl,10
div bl
; store results
mov quo,al
mov rmd,ah
; print quotient
mov dl,quo
or dl,30h
mov ah,2
int 21h
; print remainder
mov dl,rmd
or dl,30h
mov ah,2
int 21h

int 20h
d3:
; divide result by 100
mov al,rsf
mov bl,100
div bl
; store results
mov quo,al
mov rmd,ah

; print quotient
mov dl,quo
or dl,30h
mov ah,2
int 21h
; divide remainder by 10
mov al,rmd
mov bl,10
div bl

; store results
mov quo,al
mov rmd,ah

; clear registers
call clear_r

; print quotient
mov dl,quo
or dl,30h
mov ah,2
int 21h

; print remainder
mov dl,rmd
or dl,30h
mov ah,2
int 21h

int 20h

end:
int 20h
ret

n_line:
lea dx,clf
mov ah,9
int 21h
ret

clear_r:
xor ax,ax
xor bx,bx
xor cx,cx
xor cx,dx
ret

clear_v:
mov ax,3
int 10h
ret

0 comments on commit af0b6da

Please sign in to comment.