Sie sind auf Seite 1von 7

Computer Organization & Assembly Language

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.

multiply proc uses ebx ecx


mov ebx, [esp + 12]; input 1
mov ecx, [esp + 16]; input 2
mov eax, 0
multi:
add eax, ebx
loop multi
ret
multiply endp

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.

divide proc uses ebx ecx

mov ebx, [esp + 16]; Number to Divide

mov ecx, [esp + 12]; Divisor

mov eax, 0 ; Qoutient

division:

sub ebx, ecx

inc eax

cmp ebx, ecx

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.

factorial proc uses ebx ecx

mov ebx, [esp + 12] ; Number to take factorial of.

mov ecx, ebx

dec ecx

fact:

push ebx

push ecx

call multiply

add esp, 8

mov ebx, eax

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.

nPermuter proc uses ebx ecx edx


mov ebx, [esp + 20] ; n
mov ecx, [esp + 16] ; r
mov edx, ebx
sub edx, ecx ; (n-r)
push ebx
call factorial
add esp, 4
mov ebx, eax ; n!
push edx
call factorial
add esp, 4
mov edx, eax ; (n-r)!
push ebx
push edx
call divide
add esp, 8
ret
nPermuter endp

Question No. 2:

Square Root Procedure:


squareroot proc uses ebx ecx
mov ebx, lengthof matrix1 ; Length of Array
mov ecx, 1 ; Odd Number
mov eax, 0 loop1:
sub ebx, ecx
inc eax
cmp ebx, 0
jle outLoop
add ecx, 2
jmp loop1
outLoop:
ret
squareroot endp

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.

a)The multiply procedure developed in Problem 1.

multiply proc uses ebx ecx

mov ebx, [esp + 12]; input 1


mov ecx, [esp + 16]; input 2

mov eax, 0 multi:

add eax, ebx

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.

aPowern proc uses ebx ecx edx

mov ebx, [esp + 20]; a

mov ecx, [esp + 16]; n

dec ecx mov edx, ebx loop1:

push ebx

push edx

call multiply

add esp, 8

mov ebx, eax

loop loop1

mov eax, ebx

ret

aPowern endp

Question no 4: Write a program in assembly language that sorts a given integer array using
Insertion sort algorithm.

.386

.model flat, stdcall

.code

InsertionSort PROC

push EBP
mov EBP, ESP

push EBX

push ESI

push EDI

;EBP + 8 is the array

;EBP + 12 is the number of items in the array

mov EAX, [EBP+12]

mov ECX, 4

mul ECX

mov ECX, EAX

;We will move 'i' and 'j' in increments and decrements of 4,

;which is the size of the elements

mov EAX, 4 ;EAX will be our 'i'

xor EBX, EBX ;EBX will be our 'j' (setting it to 0)

mov ESI, [EBP+8] ;ESI is the array

MainLoop:

cmp EAX, ECX

jge EndLoop

push ECX

mov ECX, [ESI+EAX]

mov EBX, EAX

sub EBX, 4

EnterWhile:

cmp EBX, 0

jl EndWhile

cmp [ESI+EBX], ECX

jle EndWhile

push [ESI+EBX]

pop [ESI+EBX+4]
sub EBX, 4

jmp EnterWhile

EndWhile:

mov [ESI+EBX+4], ECX

add EAX, 4

pop ECX

jmp MainLoop

EndLoop:

pop EDI

pop ESI

pop EBX

pop EBP

RET

InsertionSort ENDP

END main

Das könnte Ihnen auch gefallen