Assembly Language Examples: Calculators, Palindrome, Loaders
Posted on Oct 16, 2024 in Computers
CALCU
Assumptions:
- ss: Stack Segment
- cs: Code Segment
- ds: Data Segment
Procedure:
Initialization:
- Display a message using a macro.
- Initialize necessary messages and variables in the code segment.
Code Segment:
- Specify the address of the data segment to the data register.
- Read the choice using the read procedure.
- Compare the choice:
- If the choice is 1: Read values into registers, add them using the add operator, call the print procedure, and jump to exit.
- If the choice is 2: Read values into registers, subtract them using the sub operator, call the print procedure, and jump to exit.
- Compare the choice (similar to step 3 for other operations).
CALCU Code
.model small
printmsg macro msg
push ax
push bx
push cx
push dx
LEA dx,msg
mov ah,09h
int 21h
pop dx
pop cx
pop bx
pop ax
endm
.data
msg1 db 0ah,0dh,"enter first number:$"
msg2 db 0ah,0dh,"enter second number:$"
msg3 db 0ah,0dh,"1-addition,2-sub,3-mul,4-div: $"
msg4 db 0ah,0dh,"enter your option :$"
msg5 db 0ah,0dh,"your result is: $"
msg6 db 0ah,0dh,"no such option $"
num dw 0
cnt dw 0
choice dw 0
a dw 0
b dw 0
.code
mov ax,@data
mov ds,ax
printmsg msg1
call readnum
mov ax,num
mov a,ax
printmsg msg2
call readnum
mov bx,num
mov b,bx
printmsg msg3
call readnum
mov ax,num
mov choice,ax
cmp choice,01
je addi
cmp choice,02
je subt
cmp choice,03
je multi
cmp choice,04
je divi
printmsg msg6
call printnum
jmp ex
addi:
printmsg msg5
mov ax,a
add ax,b
mov num,ax
call printnum
jmp ex
subt:
printmsg msg5
mov ax,a
mov bx,b
sub ax,bx
mov num,ax
call printnum
jmp ex
multi:
printmsg msg5
mov ax,a
mov bx,b
mul bx
mov num,ax
call printnum
jmp ex
divi:
printmsg msg5
mov ax,a
mov bx,b
XOR dx,dx
mov num,ax
call printnum
jmp ex
ex:
mov ah,4ch
int 21h
readnum proc near
push ax
push bx
push cx
push dx
mov num,00
r1:
mov ah,01h
int 21h
cmp al,0dh
je r2
mov cx,ax
and cx,00ffh
sub cx,30h
mov bx,10
mov ax,num
mul bx
add ax,cx
mov num,ax
jmp r1
r2:
pop dx
pop cx
pop bx
pop ax
Ret
readnum endp
printnum proc near
push ax
push bx
push cx
push dx
mov ax,num
mov bx,10
p1:
mov dx,00
div bx
push dx
inc cnt
cmp ax,00
jne p1
p2:
cmp cnt,00
je p3
pop dx
add dl,30h
mov ah,02h
int 21h
dec cnt
jmp p2
p3:
pop dx
pop cx
pop bx
pop ax
ret
printnum endp
end
Palindrome
Procedure:
- Create a string.
- Traverse to the end of the string.
- Get the address of the end of the string (DI).
- Load the starting address of the string (SI).
- Compare the value stored at the addresses.
- Increment the pointer SI.
- Decrement the pointer DI.
- Compare again the value stored at SI and DI.
- Repeat steps 5-8 until SI <= DI.
- If all the characters match, print “string is palindrome” else print “not palindrome”.
Single Level Directory
Procedure:
- Start.
- Create a structure
dir
with directory name, an array for holding filenames, and the number of files in the directory. - Read the choice from the user.
- If the choice is “create”: Read the filename from the user, add it to
dir
, and increment the file count. - If the choice is “delete”: Search for the filename. If found, delete it and decrement the file count; otherwise, print “not found”.
- If the choice is “search”: Obtain the filename. If found, print “found”; otherwise, print “File not found”.
- If the choice is “display”: If the file count is 0, print “directory empty”; otherwise, print “directory contents”.
- Stop.
Absolute Loader
Procedure:
- Begin.
- Read the header record.
- Verify the program name and length.
- Read the first text record.
- While the record type is not ‘E’:
- If the object code is in character form, convert it into internal representation.
- Move the object code to the specified location in memory.
- Read the next object program record.
- Jump to the address specified in the end record.
- End.
FCFS Scheduling
Procedure:
- Start the program.
- Initialize the variables and the size of arrays.
- Get the number of processes, burst time, and arrival time.
- Calculate the turnaround time (TT) and waiting time (WT):
- Print the turnaround time and waiting time.
- Find the average TT and average WT:
- Avg TT = Total TT / Number of processes
- Avg WT = Total WT / Number of processes
- Stop.
SJF Scheduling
Procedure:
- Start the program.
- Initialize variables and the size of arrays.
- Get the number of process IDs, burst time, and arrival time.
- Calculate TT and WT:
- Print the TT and WT.
- Calculate the average TT and average WT:
- Avg TT = Total TT / Number of processes
- Avg WT = Total WT / Number of processes
- Stop.
Even/Odd Number Check
Procedure:
- Start.
- Read a number into AX.
- Divide the number by 2.
- AX will now contain the remainder after division.
- Check if the remainder is zero or not.
- If the remainder is zero, jump to a label and print “Number is even”; otherwise, print “Number is odd”.
- Stop.