You are on page 1of 10

Lab 10 (22k-4136)

Task 1:
INCLUDE Irvine32.inc

.data

arr WORD 10, 9, 8, 7, 6, 5, 4,3,2,1


sort BYTE "Bubble Sort: ",0
gap BYTE " ",0
count DWORD ?

.code

main PROC

mov edx,OFFSET sort


call writestring
mov edx,OFFSET arr
mov ecx,LENGTHOF arr
push OFFSET arr
push ecx
call Bubble_Sort

exit
main ENDP

Bubble_Sort PROC

push ebp
mov ebp,esp

mov esi, [ebp + 12]


mov ecx,[ebp+8]
mov eax,0
mov ebx,0

L1:
mov eax,ecx
push ecx
mov esi,[ebp+12]
mov ecx,eax
sub ecx,1
cmp ecx,0
je bottom

L2:
mov bx,[esi]
cmp bx,[esi+2]
ja x1
jmp x2
x1:
mov ax,[esi+2]
mov [esi+2],bx
mov [esi],ax

x2:add esi,TYPE arr

Loop L2

pop ecx
Loop L1

bottom:

mov esi,[ebp+12]
mov ecx,LENGTHOF arr

L3:

mov ax,[esi]
call writeDEC
mov edx,OFFSET gap
call writestring
add esi ,TYPE arr
Loop L3

mov esp,ebp
pop ebp
ret
Bubble_Sort ENDP

END main

Task 2:
INCLUDE Irvine32.inc

.data

prompt BYTE "Enter a number: ",0


prompt1 BYTE "This number is Armstrong",0
prompt2 BYTE "This number is NOT Armstrong",0
count DWORD 0
var DWORD ?
sum DWORD 0
arr DWORD 5 DUP(?)

.code

main PROC

call TakeInput
exit
main ENDP

TakeInput PROC
call dumpregs
mov edx,OFFSET prompt
call writestring

mov eax,0
call readDEC
mov esi,0
mov var,eax
mov edx,0
mov ebx,0
mov ecx,eax

call Armstrong

ret
TakeInput ENDP

Armstrong PROC

call dumpregs
top: cmp eax,0
je bottom
mov eax,ecx
mov bx, 10
div bx
mov ecx,eax
mov arr[esi],edx
add esi,TYPE arr
mov edx,0
add count,1

jmp top

bottom:
mov eax,count

mov esi,0
mov ecx,count
mov edx,0

L1:
mov eax, arr[esi]
mov edx,eax
push ecx
mov ecx,count
sub ecx,1
L2:
mov eax,edx
mov ebx,arr[esi]
mul ebx
mov edx,0
mov edx,eax

Loop L2

mov arr[esi],eax
add sum,eax

pop ecx
add esi,TYPE arr

Loop L1

call Display

ret
Armstrong ENDP

Display PROC
call dumpregs
mov eax,sum
cmp eax,var
je last

mov edx,OFFSET prompt2


call writestring
call crlf
jmp x1

last:
mov edx,OFFSET prompt1
call writestring
call crlf
x1:
ret
Display ENDP
END main
Task 3:
INCLUDE irvine32.inc

.data
prompt byte "Best In the West",0

.code
main proc
mov esi,0
mov edi,lengthof prompt
sub edi,2
mov edx,offset prompt
call writestring
call crlf
call reversal

mov edx,offset prompt


call writestring
call crlf

exit
main endp

reversal proc

cmp esi,edi
jae bottom

mov dl ,prompt[esi]
mov al, prompt[edi]

mov prompt[esi],al
mov prompt[edi],dl
inc esi
dec edi
call reversal

bottom:

ret
reversal endp

end main

Task 4:
INCLUDE irvine32.inc

.data
prompt byte "Enter number: ",0
prompt2 byte"The square of the number is: ",0

.code

main PROC

call LocalSquare

exit
main ENDP

LocalSquare PROC

enter 4,0
mov edx,offset prompt
call writestring
call readDec
mov [ebp-4],eax
mov ebx,eax
mul ebx

mov edx,offset prompt2


call writeString
call writeDec

leave
ret
LocalSquare ENDP
END main

Task 5:
INCLUDE irvine32.inc

.data
prompt byte "Enter number: ",0
prompt2 byte "The factorial is: ",0

.code
main proc
mov edx,offset prompt
call writestring
call readDec

mov ebx,eax
mov eax,1

call Fact

mov edx,offset prompt2


call writestring
call writeDec

call crlf
exit
main ENDP

Fact PROC

cmp ebx,1
jz bottom

mul ebx
dec ebx

call Fact

bottom:

ret
Fact ENDP

END main
Task 6:
INCLUDE Irvine32.inc
.data
arr dword 4 dup (?)
bool byte 0

prompt byte "Enter 4 numbers: ",0


prompt2 byte "One of the numbers is non-prime.",0
prompt3 byte "The largest prime number is: ",0

.code
main PROC

mov edx,offset prompt


call writestring

mov esi,0
mov ecx,4

L1:
call readDec
mov arr[esi],eax
add esi,4

loop L1

mov ecx,4
mov edi,0

L2:
push [arr+edi]
add edi,4
call CheckPrime
cmp esi,0
je notPrime
loop L2

push arr[0]
push arr[4]
push arr[8]
push arr[12]

call LargestPrime
mov edx,offset prompt3
call writestring
call writeDec
exit

notPrime:
mov edx,offset prompt2
call writestring

exit
main ENDP

CheckPrime proc
enter 0,0

mov esi,0
mov eax,[ebp+8]
mov ebx,[ebp+8]
dec ebx
mov edx,0
top:
cmp ebx,1
jbe bottom
div ebx

cmp edx,0
je zero

dec ebx
mov eax,[ebp+8]
mov edx,0
jmp top

bottom:
mov esi,1
zero:

leave
ret
CheckPrime ENDP

LargestPrime proc
enter 0,0
mov eax,[ebp+8]

mov esi,12
mov ecx,3
L1:
cmp eax,[ebp+esi]
jae continue

mov eax,[ebp+esi]
continue:
add esi,4
loop L1

leave
ret
LargestPrime ENDP

END main

You might also like