Beruflich Dokumente
Kultur Dokumente
Assignment No 2
Question No. 1: Given n and r, write an assembly language program to find 𝑟 = 𝑛! /(𝑛−𝑟)!
a) Write a procedure named multiply that takes two numbers as input and returns their
multiplication as output. You must use addition to perform the multiplication.
b) Write a procedure named divide that takes two numbers as input and returns the
quotient as output. You must use subtraction to perform the division.
division:
inc eax
jnge endDivision
jmp division
endDivision:
ret
divide endp
c) Write a procedure named factorial that takes a number as input and returns its factorial
as output. You must use multiply procedure for multiplication.
dec ecx
fact:
push ebx
push ecx
call multiply
add esp, 8
dec ecx
cmp ecx, 0
je factOut
jmp fact
factOut:
ret
factorial endp
d) A procedure named nPermuter that takes two numbers n and r as input and returns 𝑟
You must use factorial procedure for finding factorial and divide procedure for division.
Question No. 2:
Main Procedure:
main proc
call squareroot ; To Get the Order of Matrix stored in eax mov
ecx, eax ; Loop n times
mov ebp, eax
mov ebx, 0 ; i
mov edx, 0 ; j
mov edi, 0 ; k
loop1:
push ecx
mov ecx, ebp
loop2:
push ecx
mov ecx, ebp
mov resultantmatrix[ebx + edx * 4], 0
loop3:
push ebp
push edi
call multiply
add esp, 8
mov esi, eax
push matrix1[ebx + edi]
push matrix2[esi + edx * 4]
call multiply
add resultantmatrix[ebx + edx * 4], eax
add esp, 8
add edi, 4
loop loop3
pop ecx
inc edx
cmp ebx, 4
mov edi, 0
loop loop2
pop ecx
mov edx, 0
push 4
push ebp
call multiply
add esp, 8
add ebx, eax
loop loop1
main endp
Question No. 3: Write a program that finds out 𝒂𝒏 where a and n are positive integers.
Inside the program, apart from main procedure, following two procedures must be
implemented.
loop multi
ret
multiply endp
b) Write a procedure named aPowern that takes two numbers a and n as input and returns an. You
must repeatedly use multiply procedure to get the answer.
push ebx
push edx
call multiply
add esp, 8
loop loop1
ret
aPowern endp
Question no 4: Write a program in assembly language that sorts a given integer array using
Insertion sort algorithm.
.386
.code
InsertionSort PROC
push EBP
mov EBP, ESP
push EBX
push ESI
push EDI
mov ECX, 4
mul ECX
MainLoop:
jge EndLoop
push ECX
sub EBX, 4
EnterWhile:
cmp EBX, 0
jl EndWhile
jle EndWhile
push [ESI+EBX]
pop [ESI+EBX+4]
sub EBX, 4
jmp EnterWhile
EndWhile:
add EAX, 4
pop ECX
jmp MainLoop
EndLoop:
pop EDI
pop ESI
pop EBX
pop EBP
RET
InsertionSort ENDP
END main