You are on page 1of 53

Adding two 8 bit hexadecimal numbers

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER FIRST NUMBER:','$'

MESS2 DB 0AH,0DH,'ENTER SECOND NUMBER:','$'

MESS3 DB 0AH,0DH,'SUM OF TWO 8-BIT NUMBER IS:','$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:

MOV AX,DATA

MOV DS,AX

LEA DX,MESS1

MOV AH,09H

INT 21H

CALL READ

MOV BL,DL

LEA DX,MESS2

MOV AH,09H

INT 21H

CALL READ

MOV CL,00H

ADD BL,DL

JNC NEXT

INC CL

NEXT:CALL DISP

MOV AH,4CH

INT 21H

READ PROC NEAR

MOV AH,01H

INT 21H
MOV DL,AL

MOV CL,04H

SUB DL,30H

CMP DL,0AH

JC R1

SUB DL,07H

R1:SHL DL,CL

MOV AH,01H

INT 21H

SUB AL,30H

CMP AL,0AH

JC R2

SUB AL,07H

R2:AND AL,0FH

OR DL,AL

RET

READ ENDP

DISP PROC NEAR

LEA DX,MESS3

MOV AH,09H

INT 21H

MOV DL,CL

ADD DL,30H

MOV AH,06H

INT 21H

MOV CL,04H

MOV DL,BL

SHR DL,CL

CMP DL,0AH

JC L1

ADD DL,07H
L1:ADD DL,30H

MOV AH,06H

INT 21H

AND BL,0FH

CMP BL,0AH

JC L2

ADD BL,07H

L2:ADD BL,30H

MOV DL,BL

MOV AH,06H

INT 21H

RET

DISP ENDP

CODE ENDS

END START

Addition of two 16 bit hexadecimal numbers

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER FIRST NUMBER:','$'

MESS2 DB 0AH,0DH,'ENTER SECOND NUMBER:','$'

MESS3 DB 0AH,0DH,'SUM OF TWO 16-BIT NUMBER IS:','$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:

MOV AX,DATA

MOV DS,AX

LEA DX,MESS1

MOV AH,09H

INT 21H

CALL READ

MOV BX,DX
LEA DX,MESS2

MOV AH,09H

INT 21H

CALL READ

MOV CL,00H

ADD BX,DX

JNC NEXT

INC CL

NEXT:CALL DISP

MOV AH,4CH

INT 21H

READ PROC NEAR

MOV CH,02H

R3:MOV AH,01H

INT 21H

MOV CL,04H

MOV DL,AL

SUB DL,30H

CMP DL,0AH

JC R1

SUB DL,07H

R1:SHL DL,CL

MOV AH,01H

INT 21H

SUB AL,30H

CMP AL,0AH

JC R2

SUB AL,07H

AND AL,0FH

R2:OR DL,AL

DEC CH
JZ R4

MOV DH,DL

JMP R3

R4:RET

READ ENDP

DISP PROC NEAR

LEA DX,MESS3

MOV AH,09H

INT 21H

MOV DL,CL

ADD DL,30H

MOV AH,06H

INT 21H

MOV CH,02H

L3:MOV CL,04H

MOV DL,BH

SHR DL,CL

CMP DL,0AH

JC L1

ADD DL,07H

L1:ADD DL,30H

MOV AH,06H

INT 21H

AND BH,0FH

CMP BH,0AH

JC L2

ADD BH,07H

L2:ADD BH,30H

MOV DL,BH

MOV AH,06H

INT 21H
DEC CH

JZ L4

MOV BH,BL

JMP L3

L4:RET

DISP ENDP

CODE ENDS

END START

Subtraction of two 8 bit hexadecimal numbers

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER FIRST NUMBER:','$'

MESS2 DB 0AH,0DH,'ENTER SECOND NUMBER:','$'

MESS3 DB 0AH,0DH,'SUBTRACTION OF TWO 8-BIT NUMBER IS:','$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:

MOV AX,DATA

MOV DS,AX

LEA DX,MESS1

MOV AH,09H

INT 21H

CALL READ

MOV BL,DL

LEA DX,MESS2

MOV AH,09H

INT 21H

CALL READ

MOV CL,00H
SUB BL,DL

JNC NEXT

NEG BL

MOV CL,'-'

NEXT:CALL DISP

MOV AH,4CH

INT 21H

READ PROC NEAR

MOV AH,01H

INT 21H

MOV DL,AL

MOV CL,04H

SUB DL,30H

CMP DL,0AH

JC R1

SUB DL,07H

R1:SHL DL,CL

MOV AH,01H

INT 21H

SUB AL,30H

CMP AL,0AH

JC R2

SUB AL,07H

R2:AND AL,0FH

OR DL,AL

RET

READ ENDP

DISP PROC NEAR


LEA DX,MESS3

MOV AH,09H

INT 21H

MOV DL,CL

ADD DL,30H

MOV AH,06H

INT 21H

MOV CL,04H

MOV DL,BL

SHR DL,CL

CMP DL,0AH

JC L1

ADD DL,07H

L1:ADD DL,30H

MOV AH,06H

INT 21H

AND BL,0FH

CMP BL,0AH

JC L2

ADD BL,07H

L2:ADD BL,30H

MOV DL,BL

MOV AH,06H

INT 21H

RET

DISP ENDP

CODE ENDS

END START
16 bit hexadecimal subtration

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER FIRST NUMBER :''$'

MESS2 DB 0AH,0DH,'ENTER SECOND NUMBER :''$'

MESS3 DB 0AH,0DH,'SUBTRACTION IS :''$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START: MOV AX,DATA

MOV DS,AX

LEA DX,MESS1

MOV AH,09H

INT 21H

CALL READ

MOV BX,DX

LEA DX,MESS2

MOV AH,09H

INT 21H

CALL READ

MOV CL,' '

SUB BX,DX

MOV CL,00H

JNC NEXT

INC CL

MOV CL,'-'

NEG BX

NEXT: CALL DISP

MOV AH,4CH

INT 21H

READ PROC NEAR


MOV CH,02H

R3: MOV AH,01H

INT 21H

MOV CL,04H

MOV DL,AL

SUB DL,30H

CMP DL,0AH

JC R1

SUB DL,07H

R1:SHL DL,CL

MOV AH,01H

INT 21H

SUB AL,30H

CMP AL,0AH

JC R2

SUB AL,07H

R2: AND AL,0FH

OR DL,AL

DEC CH

JZ R4

MOV DH,DL

JMP R3

R4:RET

READ ENDP

DISP PROC NEAR

LEA DX,MESS3

MOV AH,09H

INT 21H

MOV DL,CL
MOV AH,06H

INT 21H

MOV CH,02H

L3:MOV CL,04H

MOV DL,BH

SHR DL,CL

CMP DL,0AH

JC L1

ADD DL,07H

L1:ADD DL,30H

MOV AH,06H

INT 21H

AND BH,0FH

CMP BH,0AH

JC L2

ADD BH,07H

L2:ADD BH,30H

MOV DL,BH

MOV AH,06H

INT 21H

DEC CH

JZ L4

MOV BH,BL

JMP L3

L4:RET

DISP ENDP

CODE ENDS

END START
MULTIPLICATION OF TWO 8-BIT HEXADECIMAL NUMBERS

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER THE FIRST NUMBER:','$'

MESS2 DB 0AH,0DH,'ENTER THE SECOND NUMBER:','$'

MESS3 DB 0AH,0DH,'PRODUCT IS:','$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:

MOV AX,DATA

MOV DS,AX

LEA DX,MESS1

MOV AH,09H

INT 21H

CALL READ

MOV BX,DX

LEA DX,MESS2

MOV AH,09H

INT 21H

CALL READ

MOV AX,BX

MUL DL

MOV BX,AX

CALL DISP

MOV AH,4CH

INT 21H

READ PROC NEAR

MOV AH,01H
INT 21H

MOV DL,AL

MOV CL,04H

SUB DL,30H

CMP DL,0AH

JC R1

SUB DL,07H

R1:SHL DL,CL

MOV AH,01H

INT 21H

SUB AL,30H

CMP AL,0AH

JC R2

SUB AL,07H

R2:AND AL,0FH

OR DL,AL

RET

READ ENDP

DISP PROC NEAR

LEA DX,MESS3

MOV AH,09H

INT 21H

MOV CH,02H

L3:MOV CL,04H

MOV DL,BH

SHR DL,CL

CMP DL,0AH

JC L1

ADD DL,07H
L1:ADD DL,30H

MOV AH,06H

INT 21H

AND BH,0FH

CMP BH,0AH

JC L2

ADD BH,07H

L2:ADD BH,30H

MOV DL,BH

MOV AH,06H

INT 21H

DEC CH

JZ L4

MOV BH,BL

JMP L3

L4:RET

DISP ENDP

CODE ENDS

END START

MULTIPLICATION OF TWO 16-BIT NUMBERS (HEXADECIMAL)

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER FIRST NUMBER :''$'

MESS2 DB 0AH,0DH,'ENTER SECOND NUMBER :''$'

MESS3 DB 0AH,0DH,'PRODUCT IS :''$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START: MOV AX,DATA

MOV DS,AX

LEA DX,MESS1
MOV AH,09H

INT 21H

CALL READ

MOV BX,DX

LEA DX,MESS2

MOV AH,09H

INT 21H

CALL READ

MOV AX,BX

MUL DX

MOV BX,DX

MOV SI,AX

CALL DISP

MOV AH,4CH

INT 21H

READ PROC NEAR

MOV CH,02H

R3: MOV AH,01H

INT 21H

MOV CL,04H

MOV DL,AL

SUB DL,30H

CMP DL,0AH

JC R1

SUB DL,07H

R1:SHL DL,CL

MOV AH,01H

INT 21H
SUB AL,30H

CMP AL,0AH

JC R2

SUB AL,07H

R2: AND AL,0FH

OR DL,AL

DEC CH

JZ R4

MOV DH,DL

JMP R3

R4: RET

READ ENDP

DISP PROC NEAR

LEA DX,MESS3

MOV AH,09H

INT 21H

MOV CH,04H

MOV CL,04H

L3: MOV DL,BH

SHR DL,CL

CMP DL,0AH

JC L1

ADD DL,07H

L1: ADD DL,30H

MOV AH,06H

INT 21H

AND BH,0FH

CMP BH,0AH

JC L2

ADD BH,07H
L2:ADD BH,30H

MOV DL,BH

MOV AH,06H

INT 21H

DEC CH

JZ L4

MOV BH,BL

CMP CH,03H

JNZ L5

MOV DX,SI

L5:MOV BL,DH

MOV DH,DL

JMP L3

L4:RET

DISP ENDP

CODE ENDS

END START

Division of 8 bit nums

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER THE DIVIDEND:','$'

MESS2 DB 0AH,0DH,'ENTER THE DIVISOR:','$'

MESS3 DB 0AH,0DH,'DIVISION IS :','$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:

MOV AX,DATA

MOV DS,AX

LEA DX,MESS1

MOV AH,09H
INT 21H

CALL READ

MOV BL,DL

LEA DX,MESS2

MOV AH,09H

INT 21H

CALL READ

MOV AH,00H

MOV AL,BL

DIV DL

MOV BH,AL

MOV AL,00H

DIV DL

MOV BL,AL

CALL DISP

MOV AH,4CH

INT 21H

READ PROC NEAR

MOV AH,01H

INT 21H

MOV DL,AL

MOV CL,04H

SUB DL,30H

CMP DL,0AH

JC R1

SUB DL,07H

R1:SHL DL,CL

MOV AH,01H

INT 21H

SUB AL,30H

CMP AL,0AH
JC R2

SUB AL,07H

R2:AND AL,0FH

OR DL,AL

RET

READ ENDP

DISP PROC NEAR

LEA DX,MESS3

MOV AH,09H

INT 21H

MOV CH,02H

LP1:MOV CL,04H

MOV DL,BH

SHR DL,CL

CMP DL,0AH

JC L1

ADD DL,07H

L1:ADD DL,30H

MOV AH,06H

INT 21H

AND BH,0FH

CMP BH,0AH

JC L2

ADD BH,07H

L2:ADD BH,30H

MOV DL,BH

MOV AH,06H

INT 21H

DEC CH

JZ NXT

MOV DL,'.'
MOV AH,06H

INT 21H

MOV BH,BL

JMP LP1

NXT:RET

DISP ENDP

CODE ENDS

END START

Division of 16 bit nums

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER THE DIVIDEND:','$'

MESS2 DB 0AH,0DH,'ENTER THE DIVISOR:','$'

MESS3 DB 0AH,0DH,'DIVISION IS :','$'

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:

MOV AX,DATA

MOV DS,AX

LEA DX,MESS1

MOV AH,09H

INT 21H

CALL READ

MOV BX,DX

LEA DX,MESS2

MOV AH,09H

INT 21H

CALL READ

MOV SI,DX

MOV DX,0000H

MOV AX,BX
DIV SI

MOV BX,AX

MOV AX,0000H

DIV SI

MOV SI,AX

CALL DISP

MOV AH,4CH

INT 21H

READ PROC NEAR

MOV CH,02H

R3:MOV AH,01H

INT 21H

MOV CL,04H

MOV DL,AL

SUB DL,30H

CMP DL,0AH

JC R1

SUB DL,07H

R1:SHL DL,CL

MOV AH,01H

INT 21H

SUB AL,30H

CMP AL,0AH

JC R2

SUB AL,07H

AND AL,0FH

R2:OR DL,AL

DEC CH

JZ R4

MOV DH,DL

JMP R3
R4:RET

READ ENDP

DISP PROC NEAR

LEA DX,MESS3

MOV AH,09H

INT 21H

MOV CH,02H

MOV CL,04H

MOV DL,BH

SHR DL,CL

CMP DL,0AH

JC L1

ADD DL,07H

L1:ADD DL,30H

MOV AH,06H

INT 21H

AND BH,0FH

CMP BH,0AH

JC L2

ADD BH,07H

L2:ADD BH,30H

MOV DL,BH

MOV AH,06H

INT 21H

BACK:MOV DL,BL

SHR DL,CL

CMP DL,0AH

JC L3

ADD DL,07H

L3:ADD DL,30H

MOV AH,06H
INT 21H

AND BL,0FH

CMP BL,0AH

JC L4

ADD BL,07H

L4:ADD BL,30H

MOV DL,BL

MOV AH,06H

INT 21H

DEC CH

JZ NXT

MOV DL,'.'

MOV AH,06H

INT 21H

MOV BX,SI

MOV BL,BH

JMP BACK

NXT:RET

DISP ENDP

CODE ENDS

END START

Hex to binary

DATA SEGMENT

STR1 DB "BINARY NUMBER IS : $"

STR2 DB "DECIMAL NUMBER IS : $"

BSTR DB 20 DUP("$")

RSTR DB 20 DUP("$")

NEWLINE DB 13,10,"$"

CNT DB 0

N DB 2
H DB 16

D DB 10H

NUM DB ?

SNUM DB ?

HNUM DB 14H

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:

MOV AX,DATA

MOV DS,AX

;CONVERT HEXA TO DECIMAL

MOV CX,00

MOV DX,00

L6:MOV AX,00

MOV AL,HNUM

DIV D

MOV HNUM,AL

MOV BX,AX

MOV CL,CNT

MOV AX,1

L5: CMP CL,00

JE L7

MUL H

SUB CL,1

JMP L5

L7: MUL BH

ADD DX,AX
ADD CNT,1

CMP HNUM,0

JG L6

MOV NUM,DL

;CONVERT DECIMAL TO BINARY

LEA SI,BSTR

LEA DI,RSTR

L1: MOV AX,00

MOV AL,NUM

DIV N

ADD AH,30H

MOV BYTE PTR[SI],AH

INC SI

MOV NUM,AL

CMP AL,0

JG L1

DEC SI

L2:MOV BL,BYTE PTR[SI]

MOV BYTE PTR[DI],BL

DEC SI

INC DI

CMP SI,0

JNE L2

MOV AH,09H

LEA DX,STR1

INT 21H
MOV AH,09H

LEA DX,RSTR

INT 21H

MOV AH,4CH

INT 21H

CODE ENDS

END START

Hex to Decimal

name "hex"

org 100h

jmp start

source db '1c', 0

result db '000', 0

temp dw ?

start:

mov al, source[0]

cmp al, '0'

jae f1

f1:

cmp al, '9'

ja f2

sub al, 30h

jmp num1_ready

f2:

or al, 00100000b

sub al, 57h


num1_ready:

mov bl, 16

mul bl

mov temp, ax

mov al, source[1]

cmp al, '0'

jae g1

g1:

cmp al, '9'

ja g2

sub al, 30h

jmp num2_ready

g2:

or al, 00100000b

sub al, 57h

num2_ready:

xor ah, ah

add temp, ax

push temp

mov di, 2

next_digit:

cmp temp, 0

je stop

mov ax, temp

mov bl, 10

div bl

mov result[di], ah

add result[di], 30h

xor ah, ah

mov temp, ax

dec di
jmp next_digit

stop:

pop temp

mov bl, b.temp

mov cx, 8

print: mov ah, 2

mov dl, '0'

test bl, 10000000b

jz zero

mov dl, '1'

zero: int 21h

shl bl, 1

loop print

mov dl, 'b'

int 21h

mov ah, 0

int 16h

ret

favtorial

name "fact"

putc macro char

push ax

mov al, char

mov ah, 0eh

int 10h

pop ax

endm

org 100h

jmp start

result dw ?
start:

mov dx, offset msg1

mov ah, 9

int 21h

jmp n1

msg1 db 0Dh,0Ah, 'enter the number: $'

n1:

call scan_num

mov ax, 1

cmp cx, 0

je print_result

mov bx, cx

mov ax, 1

mov bx, 1

calc_it:

mul bx

cmp dx, 0

jne overflow

inc bx

loop calc_it

mov result, ax

print_result:

mov dx, offset msg2

mov ah, 9

int 21h

jmp n2

msg2 db 0Dh,0Ah, 'factorial: $'

n2:

mov ax, result

call print_num_uns

jmp exit
overflow:

mov dx, offset msg3

mov ah, 9

int 21h

jmp n3

msg3 db 0Dh,0Ah, 'the result is too big!', 0Dh,0Ah, 'use values from 0 to 8.$'

n3:

jmp start

exit:

mov ah, 0

int 16h

ret

SCAN_NUM PROC NEAR

PUSH DX

PUSH AX

PUSH SI

MOV CX, 0

MOV CS:make_minus, 0

next_digit:

MOV AH, 00h

INT 16h

MOV AH, 0Eh

INT 10h

CMP AL, '-'

JE set_minus

CMP AL, 0Dh

JNE not_cr

JMP stop_input

not_cr:

CMP AL, 8
JNE backspace_checked

MOV DX, 0

MOV AX, CX

DIV CS:ten

MOV CX, AX

PUTC ' '

PUTC 8

JMP next_digit

backspace_checked:

CMP AL, '0'

JAE ok_AE_0

JMP remove_not_digit

ok_AE_0:

CMP AL, '9'

JBE ok_digit

remove_not_digit:

PUTC 8

PUTC ' '

PUTC 8

JMP next_digit

ok_digit:

PUSH AX

MOV AX, CX

MUL CS:ten

MOV CX, AX

POP AX

CMP DX, 0

JNE too_big

SUB AL, 30h


MOV AH, 0

MOV DX, CX

ADD CX, AX

JC too_big2

JMP next_digit

set_minus:

MOV CS:make_minus, 1

JMP next_digit

too_big2:

MOV CX, DX

MOV DX, 0

too_big:

MOV AX, CX

DIV CS:ten

MOV CX, AX

PUTC 8

PUTC ' '

PUTC 8

JMP next_digit

stop_input:

CMP CS:make_minus, 0

JE not_minus

NEG CX

not_minus:

POP SI

POP AX

POP DX

RET

make_minus DB ?

SCAN_NUM ENDP
PRINT_NUM PROC NEAR

PUSH DX

PUSH AX

CMP AX, 0

JNZ not_zero

PUTC '0'

JMP printed

not_zero:

CMP AX, 0

JNS positive

NEG AX

PUTC '-'

positive:

CALL PRINT_NUM_UNS

printed:

POP AX

POP DX

RET

PRINT_NUM ENDP

PRINT_NUM_UNS PROC NEAR

PUSH AX

PUSH BX

PUSH CX

PUSH DX

MOV CX, 1

MOV BX, 10000

CMP AX, 0

JZ print_zero

begin_print:

CMP BX,0

JZ end_print
CMP CX, 0

JE calc

CMP AX, BX

JB skip

calc:

MOV CX, 0

MOV DX, 0

DIV BX

ADD AL, 30h

PUTC AL

MOV AX, DX

skip:

PUSH AX

MOV DX, 0

MOV AX, BX

DIV CS:ten

MOV BX, AX

POP AX

JMP begin_print

print_zero:

PUTC '0'

end_print:

POP DX

POP CX

POP BX

POP AX

RET

PRINT_NUM_UNS ENDP

ten DW 10
Fibonacci

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER THE LIMIT:''$'

MESS2 DB 0AH,0DH,'FIBONACCI SERIES UPTO THE LIMIT ','$'

MESS3 DB 0AH,0DH,'THE NUMBER OF ELEMENTS IN FIBONACCI SERIES IS: ','$'

MESS4 DB 'IS: ','$'

TEMP DB 01H

COUNT DB 00H

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:MOV AX,DATA

MOV DS,AX

LEA DX,MESS1

MOV AH,09H

INT 21H

CALL READ

MOV CH,DL

LEA DX,MESS2

MOV AH,09H

INT 21H

CALL READ

MOV BL,CH

CALL DISP

LEA DX,MESS4

MOV AH,09H

INT 21H

XOR BX,BX

AGAIN: INC COUNT

CALL DISP
ADD TEMP,BH

JC NEXT

MOV BL,TEMP

MOV TEMP,BH

MOV BH,BL

CMP BL,CH

JA NEXT

JMP AGAIN

NEXT:LEA DX,MESS3

MOV AH,09H

INT 21H

MOV AL,COUNT

ADD AL,00H

DAA

MOV BL,AL

CALL DISP

MOV AH,4CH

INT 21H

READ PROC NEAR

MOV CL,04H

MOV AH,01H

INT 21H

MOV DL,AL

MOV CL,04H

SUB DL,30H

CMP DL,0AH

JC R1

SUB DL,07H
R1:SHL DL,CL

MOV AH,01H

INT 21H

SUB AL,30H

CMP AL,0AH

JC R2

SUB AL,07H

R2: AND AL,0FH

OR DL,AL

RET

READ ENDP

DISP PROC NEAR

MOV DL,' '

MOV AH,06H

INT 21H

MOV DL,BL

SHR DL,CL

CMP DL,0AH

JC L1

ADD DL,07H

L1: ADD DL,30H

MOV AH,06H

INT 21H

AND BL,0FH

CMP BL,0AH

JC L2

ADD BL,07H
L2: ADD BL,30H

MOV DL,BL

MOV AH,06H

INT 21H

RET

DISP ENDP

CODE ENDS

END START

Or another fibonacci

DATA SEGMENT

MESS1 DB 0AH,0DH,'ENTER THE LIMIT:''$'

MESS2 DB 0AH,0DH,'FIBONACCI SERIES UPTO THE LIMIT ','$'

MESS3 DB 0AH,0DH,'THE NUMBER OF ELEMENTS IN FIBONACCI SERIES IS: ','$'

MESS4 DB 'IS: ','$'

TEMP DB 01H

COUNT DB 00H

DATA ENDS

CODE SEGMENT

ASSUME CS:CODE,DS:DATA

START:MOV AX,DATA

MOV DS,AX

LEA DX,MESS1

MOV AH,09H

INT 21H

CALL READ

MOV CH,DL

LEA DX,MESS2

MOV AH,09H
INT 21H

CALL READ

MOV BL,CH

CALL DISP

LEA DX,MESS4

MOV AH, 09H

INT 21H

XOR BX,BX

AGAIN: INC COUNT

CALL DISP

MOV AL,TEMP

ADD AL,BH

DAA

JC NEXT

MOV BL,AL

MOV TEMP,BH

MOV BH,BL

CMP BL,CH

JA NEXT

JMP AGAIN

NEXT:LEA DX,MESS3

MOV AH,09H

INT 21H

MOV AL,COUNT

ADD AL,00H

DAA

MOV BL,AL

CALL DISP

MOV AH,4CH

INT 21H
READ PROC NEAR

MOV CL,04H

MOV AH,01H

INT 21H

SHL AL,CL

MOV DL,AL

MOV AH,01H

INT 21H

AND AL,0FH

OR DL,AL

RET

READ ENDP

DISP PROC NEAR

MOV DL,' '

MOV AH,06H

INT 21H

MOV DL,BL

SHR DL,CL

ADD DL,30H

MOV AH,06H

INT 21H

AND BL,0FH

ADD BL,30H

MOV DL,BL

MOV AH,06H

INT 21H

RET

DISP ENDP
CODE ENDS

END START

OUTPUT

ENTER THE LIMIT: 99

FIBONACCI SERIES UPTO THE LIMIT 99 IS : 00 01 01 02 03 05 08 13 21 34 55 89

NUMBER OF ELEMENTS IN THE FIBONACCI SERIES :12

sorting

name "sort"

putc macro char

push ax

mov al, char

mov ah, 0eh

int 10h

pop ax

endm

gotoxy macro col, row

push ax

push bx

push dx

mov ah, 02h

mov dh, row

mov dl, col

mov bh, 0

int 10h

pop dx

pop bx

pop ax

endm

data segment

cr equ 0dh

lf equ 0ah
dollar equ '$'

new_line db lf, cr, dollar

msg1 db "enter first value (-32768..32767)!"

db lf, cr, dollar

msg2 db lf, cr, "enter second value (-32768..32767)!"

db lf, cr, dollar

msg3 db lf, cr, "enter third value (-32768..32767)!"

db lf, cr, dollar

msg4 db cr, lf, cr, lf, "after sorting from biggest to smallest:", dollar

msg5 db cr, lf, "num1 = ", dollar

msg6 db cr, lf, "num2 = ", dollar

msg7 db cr, lf, "num3 = ", dollar

num1 dw ?

num2 dw ?

num3 dw ?

ends

stack segment

dw 100h dup(?)

ends

code segment

start proc far

; prepare for return to os:

push ds

mov ax, 0

push ax

; set segment registers:

mov ax, data

mov ds, ax

mov es, ax

; clear the screen:

call clear_screen
; position the cursor at row=3 and column=0

gotoxy 0, 3

; ask for first number:

lea dx, msg1

call puts ; display the message.

call scan_num ; input the number into cx.

mov num1, cx

; ask for second number:

lea dx, msg2

call puts ; display the message.

call scan_num ; input the number into cx.

mov num2, cx

; ask for third number:

lea dx, msg3

call puts ; display the message.

call scan_num ; input the number into cx.

mov num3, cx

; sorting:

mov bx, num1

mov cx, num2

call sort ; exchange if bx<cx

mov num1, bx

mov num2, cx

mov bx, num2

mov cx, num3

call sort ; exchange if bx<cx

mov num2, bx

mov num3, cx

mov bx, num1


mov cx, num2

call sort ; exchange if bx<cx

mov num1, bx

mov num2, cx

; print the result:

lea dx, msg4

call puts

lea dx, msg5

call puts

mov ax, num1

call print_num ; print ax.

lea dx, msg6

call puts

mov ax, num2

call print_num ; print ax.

lea dx, msg7

call puts

mov ax, num3

call print_num ; print ax.

lea dx, new_line

call puts

mov ah, 0

int 16h

retf

start endp

puts proc near

push ax

mov ah, 09h

int 21h

pop ax

ret
endp

sort proc near

cmp bx, cx

jge compared

xchg bx, cx

compared:

ret

endp

SCAN_NUM PROC NEAR

PUSH DX

PUSH AX

PUSH SI

MOV CX, 0

; reset flag:

MOV CS:make_minus, 0

next_digit:

; get char from keyboard

; into AL:

MOV AH, 00h

INT 16h

; and print it:

MOV AH, 0Eh

INT 10h

; check for MINUS:

CMP AL, '-'

JE set_minus

; check for ENTER key:

CMP AL, 13 ; carriage return?

JNE not_cr

JMP stop_input
not_cr:

CMP AL, 8 ; 'BACKSPACE' pressed?

JNE backspace_checked

MOV DX, 0 ; remove last digit by

MOV AX, CX ; division:

DIV CS:ten ; AX = DX:AX / 10 (DX-rem).

MOV CX, AX

PUTC ' ' ; clear position.

PUTC 8 ; backspace again.

JMP next_digit

backspace_checked:

; allow only digits:

CMP AL, '0'

JAE ok_AE_0

JMP remove_not_digit

ok_AE_0:

CMP AL, '9'

JBE ok_digit

remove_not_digit:

PUTC 8 ; backspace.

PUTC ' ' ; clear last entered not digit.

PUTC 8 ; backspace again.

JMP next_digit ; wait for next input.

ok_digit:

; multiply CX by 10 (first time the result is zero)

PUSH AX

MOV AX, CX

MUL CS:ten ; DX:AX = AX*10

MOV CX, AX
POP AX

; check if the number is too big

; (result should be 16 bits)

CMP DX, 0

JNE too_big

; convert from ASCII code:

SUB AL, 30h

; add AL to CX:

MOV AH, 0

MOV DX, CX ; backup, in case the result will be too big.

ADD CX, AX

JC too_big2 ; jump if the number is too big.

JMP next_digit

set_minus:

MOV CS:make_minus, 1

JMP next_digit

too_big2:

MOV CX, DX ; restore the backuped value before add.

MOV DX, 0 ; DX was zero before backup!

too_big:

MOV AX, CX

DIV CS:ten ; reverse last DX:AX = AX*10, make AX = DX:AX / 10

MOV CX, AX

PUTC 8 ; backspace.

PUTC ' ' ; clear last entered digit.

PUTC 8 ; backspace again.

JMP next_digit ; wait for Enter/Backspace.

stop_input:

; check flag:

CMP CS:make_minus, 0

JE not_minus
NEG CX

not_minus:

POP SI

POP AX

POP DX

RET

make_minus DB ?

SCAN_NUM ENDP

PRINT_NUM PROC NEAR

PUSH DX

PUSH AX

CMP AX, 0

JNZ not_zero

PUTC '0'

JMP printed

not_zero:

CMP AX, 0

JNS positive

NEG AX

PUTC '-'

positive:

CALL PRINT_NUM_UNS

printed:

POP AX

POP DX

RET

PRINT_NUM ENDP

PRINT_NUM_UNS PROC NEAR

PUSH AX

PUSH BX
PUSH CX

PUSH DX

; flag to prevent printing zeros before number:

MOV CX, 1

; (result of "/ 10000" is always less or equal to 9).

MOV BX, 10000 ; 2710h - divider.

; AX is zero?

CMP AX, 0

JZ print_zero

begin_print:

; check divider (if zero go to end_print):

CMP BX,0

JZ end_print

; avoid printing zeros before number:

CMP CX, 0

JE calc

; if AX<BX then result of DIV will be zero:

CMP AX, BX

JB skip

calc:

MOV CX, 0 ; set flag.

MOV DX, 0

DIV BX

ADD AL, 30h

PUTC AL

MOV AX, DX

skip:

PUSH AX

MOV DX, 0

MOV AX, BX

DIV CS:ten ; AX = DX:AX / 10 (DX=remainder).


MOV BX, AX

POP AX

JMP begin_print

print_zero:

PUTC '0'

end_print:

POP DX

POP CX

POP BX

POP AX

RET

PRINT_NUM_UNS ENDP

ten DW 10

CLEAR_SCREEN PROC NEAR

PUSH AX

PUSH DS

PUSH BX

PUSH CX

PUSH DI

MOV AX, 40h

MOV DS, AX

MOV AH, 06h

MOV AL, 0

MOV BH, 07

MOV CH, 0

MOV CL, 0

MOV DI, 84h

MOV DH, [DI]

MOV DI, 4Ah

MOV DL, [DI]

DEC DL
INT 10h

MOV BH, 0

MOV DL, 0

MOV DH, 0

MOV AH, 02

INT 10h

POP DI

POP CX

POP BX

POP DS

POP AX

RET

CLEAR_SCREEN ENDP

code ends

end start

string reversal

; reverse string

name "reverse"

org 100h

jmp start

string1 db '!gnirts a si siht$'

start: lea bx, string1

mov si, bx

next_byte: cmp [si], '$'

je found_the_end

inc si

jmp next_byte

found_the_end: dec si

do_reverse: cmp bx, si

jae done

mov al, [bx]


mov ah, [si]

mov [si], al

mov [bx], ah

inc bx

dec si

jmp do_reverse

done: lea dx, string1

mov ah, 09h

int 21h

mov ah, 0

int 16h

ret

palindrome check

name "pali"

org 100h

jmp start

m1:

s db 'able was ere ere saw elba'

s_size = $ - m1

db 0Dh,0Ah,'$'

start:

mov ah, 9

mov dx, offset s

int 21h

lea di, s

mov si, di

add si, s_size

dec si

mov cx, s_size

cmp cx, 1

je is_palindrome
shr cx, 1

next_char:

mov al, [di]

mov bl, [si]

cmp al, bl

jne not_palindrome

inc di

dec si

loop next_char

is_palindrome:

mov ah, 9

mov dx, offset msg1

int 21h

jmp stop

not_palindrome:

mov ah, 9

mov dx, offset msg2

int 21h

stop:

mov ah, 0

int 16h

ret

msg1 db " this is palindrome!$"

msg2 db " this is not a palindrome!$"