21K-3172 Coal Lab 11

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

LAB 11

MUHAMMAD REHAN KHAN


21K-3172
BCS-3A
LAB 11

TASK 1
• In the following task I used scasb instruction to scan for “#” and printed the index
number in which it was present. And I used to stack to pass parameters

INCLUDE Irvine32.inc
.data
MyArray BYTE "127&j~3#^&*#*#45^",0
result BYTE "Index of The Charcater is: ",0

.code
main PROC

PUSH OFFSET MyArray


PUSH LENGTHOF MyArray
call Scan_String

exit
main ENDP

Scan_String PROC
PUSH ebp
mov ebp,esp
mov edi,[ebp+12]
mov ecx,[ebp+8]
mov ebx,[ebp+8]
mov eax,0
mov al,'#'
cld
repne scasb
jnz skip
sub ebx,ecx
dec ebx
mov edx,OFFSET result
call WriteString
mov eax, eb
call WriteDec
skip:
POP ebp
ret 8
Scan_String ENDP
End MAIN

OUTPUT:
LAB 11

TASK 2
• In the following task I used scasb instruction to scan for an user entered character and I
passed parameters through stack and printed the index number in which it was present.

INCLUDE Irvine32.inc
.data
str1 BYTE "127&j~3#^&*#*#45^",0
msg BYTE "Enter Character to find: ",0
msg1 byte "Character found at index: ",0
.code
main PROC
push offset str1
push lengthof str1
mov edx,offset msg
call writestring
call scan_string
exit
main ENDP
scan_string PROC
push ebp
mov ebp,esp
mov edi,[ebp+12]
call readchar
call writechar
call crlf
cld
mov ecx,[ebp+8]
repne scasb
jnz _End
mov eax,[ebp+8]
sub eax,ecx
dec eax
mov edx,offset msg1
call writestring
call writedec
_End:
pop ebp
ret
scan_string ENDP
END Main

OUTPUT:
LAB 11

TASK 3
• In the following task I called str_compare function within my function to compare two
strings according to their ascii codes and printed which string is greater.
INCLUDE Irvine32.inc
.data
str1 BYTE "ab",0
str2 BYTE "abc",0
msg1 byte "soucre is bigger",0
msg2 byte "target is bigger",0
.code
isCompare PROTO ptr1:dword , ptr2:dword
main PROC
invoke isCompare, addr str1,addr str2
exit
main ENDP
isCompare PROC ptr1:dword , ptr2 :dword
mov esi,ptr1
mov edi,ptr2
invoke str_compare,addr str1 ,addr str2
JA source
mov edx,offset msg2
call writestring
jmp _end
source:
mov edx,offset msg1
call writestring
call crlf
_end:
ret
isCompare ENDP
END Main

OUTPUT:
LAB 11

TASK 4
• I used the most common approach for reversing a string by pushing it into a stack and
then popping it into the string again. Hence having the string reversed.
INCLUDE irvine32.inc
.data
msg BYTE "COAL",0
.code
str_reverse PROTO ptr1:dword, a:dword
main PROC
invoke str_reverse, addr msg ,lengthof msg
mov ecx,lengthof msg
mov eax,0
mov esi,offset msg
l1:
mov al,[esi]
inc esi
call writechar
loop l1

exit
main ENDP
str_reverse PROC ptr1:dword, a:dword
mov esi,ptr1
mov ecx,a
l1:
push [esi]
inc esi
loop l1
mov ecx,a
mov esi,ptr1
l2:
pop [esi]
inc esi
loop l2
ret
str_reverse ENDP
END main

OUTPUT:
LAB 11

TASK 5
• In the following task I asked used to enter byte number and than multipled the byte by 4
as array was of dword and printed the element on the byte accordingly.
INCLUDE irvine32.inc
.data
arr dword 1,2,3,4,5
msg byte "Enter Byte Number: ",0
.code
load PROTO ptr1:dword, a:dword
main PROC
mov edx,offset msg
call writestring
call readdec
invoke load, addr arr , eax
exit
main ENDP
load PROC ptr1:dword, a:dword
mov esi,ptr1
mov eax,a
mov ecx,4
mul ecx
mov ecx,eax
mov eax,[esi+ecx]
call writedec
ret
load ENDP
END main

OUTPUT:
LAB 11

TASK 6
• In the following task I incremented the number of times a letter occurred in a string (by
using conditional jumps) in freqtable array on its ascii value index for doing so I
multiplied the ascii by 4 as my freqtable array was of dword type.
INCLUDE irvine32.inc
.data
target byte "AAEBDCFBBC",0
freqTable DWORD 256 DUP(0)
space byte " ",0
.code
Get_Frequencies PROTO ptr1:dword, ptable:dword
main PROC
invoke Get_Frequencies, addr target,addr freqTable
mov ecx,lengthof freqTable
mov esi,offset freqTable
l1:
mov eax,[esi]
call writedec
mov edx,offset space
call writestring
add esi, type freqTable
loop l1
exit
main ENDP
Get_Frequencies PROC ptr1:dword, ptable:dword
mov esi,ptr1
mov edi,ptable
cld
l1:
mov eax,0
lodsb
cmp al,0
je _exit
shl eax,2
inc dword ptr [edi+eax]
loop l1
_exit:
ret
Get_Frequencies ENDP
END main

OUTPUT:

You might also like