0% found this document useful (0 votes)
87 views

Example: Array Scanning Searching

1. The document contains examples of assembly language programs that demonstrate various programming concepts like arrays, loops, procedures, recursion, encryption, and more. 2. The examples include programs for scanning an array, encryption/decryption, calculating factorials recursively, and procedures with local variables. 3. Additional examples provided are for tasks like reversing an array in-place without copying elements, reading/encrypting/decrypting a file, and displaying ASCII characters and values.

Uploaded by

Yaikob Kebede
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Example: Array Scanning Searching

1. The document contains examples of assembly language programs that demonstrate various programming concepts like arrays, loops, procedures, recursion, encryption, and more. 2. The examples include programs for scanning an array, encryption/decryption, calculating factorials recursively, and procedures with local variables. 3. Additional examples provided are for tasks like reversing an array in-place without copying elements, reading/encrypting/decrypting a file, and displaying ASCII characters and values.

Uploaded by

Yaikob Kebede
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

lab manual and assignment

Example: Array scanning searching


TITLE Scanning an Array (ArryScan.asm)
; Scan an array for the first nonzero value.
INCLUDE Irvine32.inc
.data
intArray SWORD 0,0,0,0,-1,20,35,-12,66,4,0
;intArray SWORD 1,0,0,0 ; alternate test data
;intArray SWORD 0,0,0,0 ; alternate test data
;intArray SWORD 0,0,0,1 ; alternate test data
noneMsg BYTE "A non-zero value was not found",0
.code
main PROC
mov ebx,OFFSET intArray ; point to the array
mov ecx,LENGTHOF intArray; loop counter
L1: cmp WORD PTR [ebx],0 ; compare value to zero
jnz found ; found a value
add ebx,2 ; point to next
loop L1 ; continue the loop
jmp notFound ; none found
found: ; display the value
movsx eax,WORD PTR[ebx] ; sign-extend into EAX
call WriteInt
jmp quit
notFound: ; display "not found" message
mov edx,OFFSET noneMsg
call WriteString
quit:
call Crlf
exit
main ENDP
END main

Example: Encryption system

TITLE Encryption Program (Encrypt.asm)


INCLUDE Irvine32.inc
KEY = 239 ; any value between 1-255
BUFMAX = 128 ; maximum buffer size
.data
sPrompt BYTE "Enter the plain text:",0
sEncrypt BYTE "Cipher text: ",0
sDecrypt BYTE "Decrypted: ",0
buffer BYTE BUFMAX+1 DUP(0)
bufSize DWORD ?
.code
main PROC
call InputTheString ; input the plain text
call TranslateBuffer ; encrypt the buffer
mov edx,OFFSET sEncrypt ; display encrypted message
call DisplayMessage
call TranslateBuffer ; decrypt the buffer
mov edx,OFFSET sDecrypt ; display decrypted message
call DisplayMessage
exit
main ENDP
;-----------------------------------------------------InputTheString PROC
;
; Prompts user for a plaintext string. Saves the string
; and its length.
; Receives: nothing
; Returns: nothing
;-----------------------------------------------------pushad ; save 32-bit registers
mov edx,OFFSET sPrompt ; display a prompt
call WriteString
mov ecx,BUFMAX ; maximum character count
mov edx,OFFSET buffer ; point to the buffer
call ReadString ; input the string
mov bufSize,eax ; save the length
6.3 Conditional Jumps 199
call Crlf
popad
ret
InputTheString ENDP
;-----------------------------------------------------DisplayMessage PROC
;
; Displays the encrypted or decrypted message.
; Receives: EDX points to the message
; Returns: nothing
;-----------------------------------------------------pushad
call WriteString
mov edx,OFFSET buffer ; display the buffer
call WriteString
call Crlf
call Crlf
popad
ret
DisplayMessage ENDP
;-----------------------------------------------------TranslateBuffer PROC
;
; Translates the string by exclusive-ORing each
; byte with the encryption key byte.
; Receives: nothing
; Returns: nothing
;-----------------------------------------------------pushad
mov ecx,bufSize ; loop counter
mov esi,0 ; index 0 in buffer
L1:
xor buffer[esi],KEY ; translate a byte
inc esi ; point to next byte
loop L1
popad
ret
TranslateBuffer ENDP
END main

Example: Recursive procedure

TITLE Calculating a Factorial (Fact.asm)


INCLUDE Irvine32.inc
.code
main PROC
push 5 ; calc 5!
call Factorial ; calculate factorial (EAX)
call WriteDec ; display it
call Crlf
exit
main ENDP
;----------------------------------------------------Factorial PROC
; Calculates a factorial.
; Receives: [ebp+8] = n, the number to calculate
; Returns: eax = the factorial of n
;----------------------------------------------------push ebp
mov ebp,esp
mov eax,[ebp+8] ; get n
cmp eax,0 ; n 0?
ja L1 ; yes: continue
mov eax,1 ; no: return 1 as the value of 0!
jmp L2 ; and return to the caller
L1: dec eax
push eax ; Factorial(n1)
call Factorial
; Instructions from this point on execute when each
; recursive call returns.
ReturnFact:
mov ebx,[ebp+8] ; get n
mul ebx ; EDX:EAX = EAX * EBX
L2: pop ebp ; return EAX
ret 4 ; clean up stack
Factorial ENDP
END main

Example: procedure with local variables


TITLE Swap Procedure Example (Swap.asm)
INCLUDE Irvine32.inc
Swap PROTO, pValX:PTR DWORD, pValY:PTR DWORD
.data
Array DWORD 10000h,20000h
.code
main PROC
; Display the array before the exchange:
mov esi,OFFSET Array
mov ecx,2 ; count = 2
mov ebx,TYPE Array
call DumpMem ; dump the array values
INVOKE Swap, ADDR Array, ADDR [Array+4]
; Display the array after the exchange:
call DumpMem
exit
main ENDP
;-------------------------------------------------------Swap PROC USES eax esi edi,
pValX:PTR DWORD, ; pointer to first integer
pValY:PTR DWORD ; pointer to second integer
;
; Exchange the values of two 32-bit integers
; Returns: nothing
;-------------------------------------------------------mov esi,pValX ; get pointers
mov edi,pValY
mov eax,[esi] ; get first integer
xchg eax,[edi] ; exchange with second
mov [esi],eax ; replace first integer
ret ; PROC generates RET 8 here
Swap ENDP
END main

Micro Processor And Assembly Programming Language Assignment.


1. Use a loop with indirect or indexed addressing to reverse the elements of an integer array in
place. Do not copy the elements to any other array. Use the SIZEOF, TYPE, and LENGTHOF
operators to make the program as flexible as possible if the array size and type should be
changed in the future.
2. write assembly program that read any text and encrypt and decrypt the file (you can do
both encrypt and decrypt in one program).
3. write simple program that display the ASCII characters and their corresponding value. eg
A:=65
4. Create an array of randomly ordered integers. Using the Swap example given in this manual
as a reference, write a loop that exchanges each consecutive pair of integers in the array.

You might also like