Documente Academic
Documente Profesional
Documente Cultură
.data
msg1 db "Enter first number $"
msg2 db "Enter second number: $"
msg3 db "Difference btw two numbers: $"
num1 db ?
num2 db ?
.code
main proc
mov ax, @data ;load data
mov ds, ax
lea dx, msg1
mov ah, 9
int 21h ;display the msg1
mov ah, 1
int 21h ;read a single digit number
sub al, 30h ;convert decimal to ascii
mov num1, al ;save first number in num1
mov ah, 2
mov dl, 0dh
int 21h ;cursor to begining
mov dl, 0ah
int 21h ;new line
lea dx, msg2
mov ah, 9
int 21h ;display the msg2
mov ah, 1
int 21h ;read a single digit number
sub al, 30h ;convert decimal to ascii
mov num2, al ;save second digit in num2
mov ah, 2
mov dl, 0dh
int 21h ;cursor to begining
mov dl, 0ah
int 21h ;new line
lea dx, msg3
mov ah, 9
int 21h ;display the msg3
mov bl, num1
mov bh, num2 ;load values for comparison
cmp bl, bh ;check if first number at num1 is larger
jg diff ;if yes jump to difference
xchg bl, bh ;else exhange the values
mov num1, bl
mov num2, bh ;store the values in order (Doing this ext
ra step for showing the variable use)
diff:
mov al, num1
sub al, num2 ;do the actual substraction
add al, 30h ;convert ascii to decimal
mov ah, 2
mov dl, al
int 21h ;display the character
mov ah, 4ch ;return control to os
int 21h
main endp
end main
code segment
assume cs: code, ds: data
start:
mov ax, data
mov ds, ax
mov es, ax
lea dx, fname
mov cx, 0
mov ah, 3ch
int 21h
mov fhand, ax
lea dx, fname
mov al, 0
mov ah, 3dh
int 21h
lea dx, fdata
mov cx, '$'-fdata
mov bx, fhand
mov ah, 40h
int 21h
mov ah, 3eh
int 21h
mov ah, 4ch
int 21h
code ends
end start
3. Sort an Array.
Code:
data segment
arr db 05h, 08h, 03h, 02h, 01h, 09h, 07h, 04h
siz equ 7
data ends
code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov dx, siz
oloop:
mov cx, dx
lea si, arr
iloop:
mov al, [si]
cmp al, [si + 1]
jl eloop
xchg al, [si + 1]
mov [si], al
eloop:
add si, 1
loop iloop;
dec dx
jnz oloop;
mov ah, 4ch
int 21h
code ends
end start
lim dw ?
mov dx, offset msg
mov ah, 9
int 21h ;print title
mov dx, offset msg1
mov ah, 9
int 21h
call scan_num ;get lim in cx
mov CX, CX ;store cx again cx to solve unwanted char
mov BH, 01h
mov DH, 01h
mov DL, 00h ;clear DL to solve unwanted char
lea si, msg2
call print_string ;print series message
fibLoop: ;30h for ascii zero
or DL, 30h
mov AH, 02h
int 21h ;print series chars
mov DL, DH
mov DH, BH
push DX
mov AL, DL
mov AH, DH
add AH, AL
mov BH, AH
pop DX
loop fibLoop
mov ah, 0
int 16h ; wait for any key press to exit
ret
msg db "Fibonacci Series (Single Digit)$"
msg1 db 13,10, "Enter limit(1-7): $"
msg2 db 13,10, "Series : ", 0
define_scan_num
define_print_num
define_print_num_uns
define_print_string
end
code segment
assume cs: code, ds: data
start:
mov ax, data
mov ds, ax
lea dx, input
mov ah, 09h
int 21h
lea si, bcd
add si, 09h
; Get the input character by character
mov bs, 00h
inp:
mov ah, 01h
int 21h
cmp al, 0dh
je fininp
cmp al, '0'
jl fininp
cmp al, '9'
jg fininp
inc bs
sub al, 30h
mov [si], al
dec si
jmp inp
; Get the next multiplier
proc mplier near
push ax
push bx
push cx
push dx
mov ax, mult
cmp ax, 0000h
jne mply
mov ax, 000ah
mov mult, ax
jmp emplier
mply:
mov bx, 000ah
mov dx, 0000h
mul bx
mov mult, ax
emplier:
pop dx
pop cx
pop bx
pop ax
ret
mplier endp
; Convert the array like -- 01h 06h 09h -- to 169 = A9H
fininp:
lea si, bcd
add si, 09h
mov ch, 00
mov cl, bs
sub si, cx
inc si
mov dno, 0000
mov ax, 0000h
add al, [si]
mov dno, ax; Store the last digit in the memory
mov ch, 00
mov cl, bs
dec cx
getdec:
inc si
mov ax, 0000h
mov al, [si]
call mplier; Get the next multiplier
mov bx, mult
mul bx
add dno, ax
loop getdec
; Next line stuff
mov dl, 0bh
mov ah, 02h
int 21h
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
; Print output
lea dx, output
mov ah, 09h
int 21h
; Convert and store printable string in the memory
convert:
mov ax, dno
mov bx, 0002h; divisor
lea di, bin
add di, 19h; Go to the end of bin
mov [di], '$'; String ending
dec di
mov cx, 0000; Store number of binary digits, better for printi
ng
rpconv:
mov dx, 0000h
div bx; repeated division
inc cx
add dl, '0'; ASCII value
mov [di], dl
dec di
and ax, ax
jnz rpconv
lea dx, bin
add dx, 19h
sub dx, cx; Adjust the digits
mov ah, 09h
int 21h; Print the number
; Store the result in bx, cx, and dx for the glory of Satan
mov bx, dno
mov cx, dno
mov dx, dno
mov ah, 4ch
int 21h
code ends
end start