Assembly Questions

Assembly Questions
1. Write an assembly program that reads a string (HELLO) ,put it in an array named
hello and print it using string printing method.
.model small
.stack 100h
.data
hello db 6 dup (?)
ENTER DB 0AH,0DH,'$' ;to print new line
.code
main proc
mov ax,@data
mov ds,ax
;---------------------------mov cx,5 ;set the counter
mov bx,0 ;start index for array hello
hello1:
mov ah,01h
int 21h
mov hello[bx],al
add bx,1 ;increment the index by 1
Loop hello1
mov hello[5],'$' ;add $ to end the string
;printing new line after reading
lea dx,ENTER
mov ah,09h
int 21h
;print hello
lea dx,hello
mov ah,09h
int 21h
;--------------------------mov ax,4c00h
int 21h
main endp
end main
2. Write an assembly program that reads two values of one digit and find the sum of
them ,print the output value in decimal.
.model small
.stack 100h
.data
var1 db ?
var2 db ?
result db ?
msg1 db 'Enter first number: $'
msg2 db 'Enter second number: $'
msg3 db 'Result= $'
ENTER DB 0AH,0DH,'$'
.code
main proc
mov ax,@data
mov ds,ax
lea dx,msg1
mov ah,09h
int 21h
mov ah,01h
int 21h
sub al,48
mov var1,al
lea dx,Enter
mov ah,09h
int 21h
lea dx,msg2
mov ah,09h
int 21h
mov
int
mov
sub
ah,01h
21h
var2,al
var2,48
mov
add
add
mov
al,var1
al,var2
al,'0'
result,al
lea dx,Enter
mov ah,09h
int 21h
lea dx,msg3
mov ah,09h
int 21h
mov ah,0eh
mov al,result
int 10h
mov ax,4c00h
int 21h
main endp
end main
3. Write a program that reads 5 values of one digit, put them in array, sums them ,
print the resultant value
.MODEL SMALL
.STACK 64H
.data
message DB 10,13,'Enter 5 values: $'
result DB 10,13,'Sum= $'
arr DB 5 DUP (?)
Res DB ?
.code
;initialize DS
main proc
MOV
AX,@DATA
MOV
DS,AX
;-------------------
;printing message
LEA DX,message
MOV AH,09H
INT 21H
;reading the numbers
MOV CX,5
MOV bx,0
READ:
MOV AH,01H
INT 21H
MOV arr[BX],AL
SUB arr[BX],48
ADD BX,1
INT 21H; to read spaces between numbers
LOOP READ
;Sum the numbers
MOV AL,0
MOV CX,5
MOV bx,0
ADDITION:
ADD AL,arr[BX]
ADD BX,1
LOOP ADDITION
ADD AL,48
MOV Res,AL
;Print the result
LEA DX,result
MOV AH,09H
INT 21H
MOV AL,Res
MOV AH,0EH
INT 10H
;------------------;exit to DOS
MOV
AX,4C00h
INT
21h
main endp
END main
•Write a program that reads a small character and prints out the capital value
of it.
.MODEL SMALL
.STACK 64H
.data
message DB 10,13,'Enter small letter: $'
result DB 10,13,'Capital is $'
.code
;initialize DS
main proc
MOV
AX,@DATA
MOV
DS,AX
;------------------;printing message
LEA DX,message
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
MOV BL,AL
SUB BL,32
LEA DX,RESULT
MOV AH,09H
INT 21H
MOV AL,BL
MOV AH,0EH
INT 10H
;exit to DOS
MOV
AX,4C00h
INT
21h
main endp
END main
•Write a program that asks the user to enter a value (less than 10) then find
the factorial of that value.
title fact
.model small
.stack 100h
.data
VAR DW ?
ENTER DB 10,13,'$'
msg db 'Enter a number: $'
res db '!= $'
ten dw 10
.code
main proc
;initialize DS
MOV
AX,@DATA
MOV
DS,AX
;------------------;print the message: 'Enter a number:'
lea dx,msg
mov ah,09h
int 21h
;read the number
MOV AH,01H
INT 21H
;to keep read value from lost
MOV BL,AL
;to print enter
LEA DX,enter
MOV ah,09H
int 21h
;to print the read number
MOV al,bl
mov ah,0Eh
int 10h
;to calculate the actual value of entered number
SUB BL,'0'
;to print the message: '!= '
LEA dx,res
mov ah,09h
int 21h
;set the value of counter to be as the entered number
MOV CX,0
MOV CL,BL
;calculate the factorial result is stored in AX
MOV AX,1
Factorial:
MUL CX; ax=ax*cx
LOOP Factorial
mov cx,5
mov var,10000
loop1:
mov
div
add
mov
int
mov
dx,0
var
al,'0'
ah,0eh
10h
bx,dx
;to
mov
mov
div
mov
update var from 10000 to 1000 to 100 to 10 to 1
dx,0
ax,var
ten
var,ax
mov ax,bx
loop loop1
;to print enter
LEA DX,enter
MOV ah,09H
int 21h
;------------------;exit to DOS
MOV
INT
main endp
end main
AX,4C00h
21h
•Write a program that find the sum of an array elements and store the result in
AX.
.model small
.stack 100h
.data
arr db 1,3,5,23,34,3,6
res db 'Sum= ','$'
var dw ?
ten dw 10
.code
main proc
;initialize DS
MOV
AX,@DATA
MOV
DS,AX
;------------------mov al,0
mov cx,7
mov bx,0
sum:
add al,arr[bx]
inc bx
loop sum
mov ah,0
mov bx,ax
lea dx,res
mov ah,09h
int 21h
mov ax,bx
mov cx,5
mov var,10000
loop1:
mov
div
add
mov
int
mov
dx,0
var
al,'0'
ah,0eh
10h
bx,dx
;to
mov
mov
div
mov
update var from 10000 to 1000 to 100 to 10 to 1
dx,0
ax,var
ten
var,ax
mov ax,bx
loop loop1
;-------------------
;exit to DOS
MOV
AX,4C00h
INT
21h
main endp
end main
•Write a program that asks the user to enter two values (less than 10) and
store them in variables named NUMand POWthen find the result of the (
NUMPOW)and store it in variable named RESULT.
( for example: if NUM=3 and POW=2 then Result= 32= 9 )
.model small
.stack 100h
.data
var dw ?
msg db 'Enter a number: $'
ENTER DB 10,13,'$'
NUM db ?
POW db ?
RESULT dw ?
ten dw 10
.code
main proc
MOV
MOV
AX,@DATA
DS,AX
; Enter the first number and store it into NUM
lea dx,msg
mov ah,09h
int 21h
MOV AH,01H
INT 21H
MOV NUM,AL
LEA DX,enter
MOV ah,09H
int 21h
SUB NUM,'0'
;_______________
; Enter the second number and store it into POW
lea dx,msg
mov ah,09h
int 21h
MOV AH,01H
INT 21H
MOV POW,AL
LEA DX,enter
MOV ah,09H
int 21h
SUB POW,'0'
;________________
LEA DX,enter
MOV ah,09H
int 21h
;set the value of counter to be as the POW
MOV CX,0
MOV CL,POW
;calculate the NUM ^ POW
MOV AX,1
power:
MUL NUM
LOOP power
MOV RESULT,AX
mov cx,5
mov var,10000
loop1:
mov
div
add
mov
int
mov
dx,0
var
al,'0'
ah,0eh
10h
bx,dx
;to
mov
mov
div
mov
update var from 10000 to 1000 to 100 to 10 to 1
dx,0
ax,var
ten
var,ax
mov ax,bx
loop loop1
LEA DX,enter
MOV ah,09H
int 21h
MOV
INT
main endp
end main
AX,4C00h
21h
•Write a program that’s asks the user to input two values then the program will
output if these two values are equal, the first value is greateror the second
value is greaterthan the first value.
•Example :
Enter the First Value : 4
Enter the Second value : 7
The SECOND value is greater.
•Modify your program to compare between two digits values. (for example
between 59 and 12)
.model small
.stack 100h
.data
msg1 db 'Enter the First Value: $'
msg2 db 10,13,'Enter the second value: $'
eq1 db 10,13,'Equal$'
grt1 db 10,13,'The first value is greater$'
grt2 db 10,13,'The second value is greater$'
var1 db ?
var2 db ?
.code
main proc
;initialize DS
MOV
AX,@DATA
MOV
DS,AX
;------------------;read first value
lea dx,msg1
mov ah,09h
int 21h
;read first digit of first value
mov ah,01h
int 21h
sub al,48
mov bl,10
mul bl ; multiply by 10
mov var1,al
mov
int
sub
add
ah,01h
21h
;read second digit of first value
al,48
var1,al ;add second digit to first digit after multiply it
by 10
;read second value
lea dx,msg2
mov ah,09h
int 21h
mov
int
sub
mov
mul
mov
ah,01h
21h
al,48
bl,10
bl
var2,al
mov
int
sub
add
ah,01h
21h
al,48
al,var2
cmp var1,al
JE equal ; if var1 = al jump to equal label
JG great ;if var1 > al jump to great label
;else then var1 < al
lea dx,grt2
mov ah,09h
int 21h
jmp exit
equal:
lea
mov
int
jmp
dx,eq1
ah,09h
21h
exit
great:
lea dx,grt1
mov ah,09h
int 21h
EXIT:
;------------------;exit to DOS
MOV
AX,4C00h
INT
21h
main endp
end main
•Write a program that’s asks the user to input a number of two digits then the
program will output if the number is evenor odd.
•Samples of the program execution :
Enter your number : 83
Your number is ODD.
Enter your number : 54
Your number is EVEN.
•Modify your program so that the execution of the program will be repeated
until the user input Qinstead of the number
.model small
.stack 100h
.data
MSG1 DB 10,13,'ENTER YOUR NUMBER: $'
MSG2 DB 10,13,'THE NUMBER IS EVEN$'
MSG3 DB 10,13,'THE NUMBER IS ODD$'
.code
main proc
;initialize DS
MOV
AX,@DATA
MOV
DS,AX
;------------------;My Code
START:
lea dx,msg1
mov ah,09h
int 21h
MOV AH,01H
INT 21H
MOV CL,AL ;read first digit
CMP CL,'Q'; if read value = Q then exit
JE EXIT
MOV
INT
MOV
MOV
AH,01H
21H
DX,0
DL,AL ;read second digit
SUB CL,48 ;convert to actual value
SUB DL,48
MOV AL,10
MUL CL
;multiply first digit by 10
ADD AX,Dx ;add first digit to second digit to formulate the whole
number
MOV DX,0
MOV BX,2
DIV BX
CMP DX,0 ;to test if num%2 = 0
JZ EV
JNZ ODD
EV:
lea
mov
int
JMP
dx,msg2
ah,09h
21h
STOP
ODD:
lea dx,msg3
mov ah,09h
int 21h
JMP STOP
STOP:
JMP START ; go to the begining to read another value
EXIT:
;------------------;exit to DOS
MOV
AX,4C00h
INT
21h
main endp
end main
•Write a program that’s asks the user to input a password of 5 characters then
the program will compare the input password with a stored password and if
they are equal the program will output “WELCOME” , if they are not equal give
the user 3 chances to enter the correct password after that terminate your
program.
title project
.model small
.stack 100h
.data
msg1 db 10,13,'Enter Password: $'
wel db 10,13,'Welcome$'
pass db 'hello$'
pass2 db 6 dup (?)
pcount db ?
.code
main proc
mov ax,@data
mov ds,ax
mov pass2[5],'$'
mov pcount,0
enter:
mov bx,0
mov cx,5
lea
mov
int
mov
password:
dx,msg1
ah,09H
21h
ah,01h
int 21h
mov pass2[bx],al
inc bx
loop password
mov bx,0
mov cx,5
compareP:
mov al,pass[bx]
cmp al,pass2[bx]
jnz wrong
inc bx
loop compareP
lea dx,wel
mov ah,09H
int 21h
jmp exit
wrong:
inc pcount
cmp pcount,3
jz exit
jmp enter
exit:
mov ax,4c00h
int 21h
main endp
end main
•Write a program that’s asks the user to input a number of two digits then the
program will output the squareof that number.
•Samples of the program execution :
Enter your number : 12
The square of your number is 144.
Enter your number : 20
The square of your number is 400.
.MODEL SMALL
.STACK 100H
.data
msg1 db 10,13,'Enter your number: $'
msg2 db 10,13,'The square of your number is: $'
var DW 0
RES1 DW ?
res db 4 dup (?)
.code
main proc
MOV
AX,@DATA
MOV
DS,AX
;--------------lea dx,msg1
mov ah,09h
int 21h
mov
int
mov
int
mov
ah,01h
21h
bl,al ;read first digit and store it in bl
21h
cl,al ;read second digit and store it in cl
sub
sub
mov
mul
add
mov
bl,48
cl,48
al,10
bl
al,cl
var,al
; mul first digit by 10 and all second digit to it
;find the square
mul var ; ax = al*var
;print the result (the result will be maximum of 4 digits)
MOV RES1,AX
lea dx,msg2
mov ah,09h
int 21h
mov var,10000 ;to seprate digits
MOV CX,5 ;loop 4 times
MOV BX,10; to update the value of var form 1000 to 100 to 10 to
1
RESULT:
MOV AX,RES1 ;mov the result of square to ax
MOV DX,0
;in order to divide (DX AX) on var dx must be
zero
DIV
ADD
MOV
INT
MOV
MOV
VAR
AL,48
AH,0EH
10H
RES1,DX
DX,0
;AX = (DX AX)/VAR and DX the remainder
;print result of division
;copy the remainder of division back to RES1
;in order to divide (DX AX) on var dx must be
zero
MOV AX,VAR
DIV BX
MOV VAR,AX
;
;to update the value of var
LOOP RESULT
exit:
MOV
AX,4C00h
INT
21h
main endp
END main