Electric Network Analysis Lab
Electric Network Analysis Lab
Signature: ____________________________
1
EE243L: Microcontrollers and Interfacing Lab
Lab goals
This lab will enable students to achieve the following:
Use stack memory using PUSH and POP instructions to write assembly language programs
Define procedures and call these procedures to perform certain tasks in program
Learn how to use Irvine library procedures to input and output data on console screen
Implement logical problems using conditional jump statements
Lab conduct
1. You have to perform this experiment using Visual Studio 2015 and Irvine libraries for MASM.
2. You are required to work individually; everyone must attempt to understand the purpose of assembly language
coding and simulated set up for the given experiment.
3. In case some aspect of the lab experiment is not understood,you are advised to seek help from the instructor, lab
engineer or the TA.
Lab details
4.1 Stack operations: PUSH and POP
4.1.1 Runtime stack
“Stack” is a section of main memory that is accessible to the processor in a Last-In, First-Out (LIFO) manner.
The runtime stack is a memory array managed directly by the CPU, using the ESP (Extended Stack Pointer) register. In
32-bit mode, ESP register holds a 32-bit offset into some location on the stack. We rarely manipulate ESP directly;
instead, it is indirectly modified by instructions such as CALL, RET, PUSH, and POP. ESP always points to the last
value to be added to, or pushed on to the top of stack.
There are several important uses of runtime stacks in programs:
A stack makes a convenient temporary save area for registers when they are used for more than one purpose. After
they are modified, they can be restored to their original values.
When the CALL instruction executes, the CPU saves the current subroutine’s return address on the stack.
When calling a subroutine, you pass input values called arguments by pushing them on the stack.
The stack provides temporary storage for local variables inside subroutines.
4.1.2 PUSH instruction
The PUSH instruction first decrements ESP and then copies a source operand into the stack. A 16-bit operand causes
ESP to be decremented by 2. A 32-bit operand causes ESP to be decremented by 4. There are three instruction formats:
PUSH reg/mem16
PUSH reg/mem32
PUSH imm32
2
Lab-4
3
EE243L: Microcontrollers and Interfacing Lab
4
Lab-4
Mnemonic Description
JZ Jump if zero
JNZ Jump if not zero
JC Jump if carry
JNC Jump if not carry
JL Jump if lower for signed comparison
JG Jump if greater for signed comparison
JE Jump if equal
JNE Jump if not equal
5
EE243L: Microcontrollers and Interfacing Lab
Lab tasks
1. The following program sums a DWORD integer array and stores the result in a new variable “theSum”. It uses a
procedure “ArraySum” that is defined below the main procedure. You are required to run this program in debugging
mode line-by-line and record the changes in relevant registers after each line executes. Ignore the lines that are not
executed (data and directive lines).
Statement Registers
.386
.model flat, stdcall
.stack 4096
ExitProcess PROTO,
dwExitCode:DWORD
.data
array DWORD 10000h,
20000h, 30000h, 40000h,
50000h
theSum DWORD ?
.code
main PROC
mov esi,OFFSET array EIP = 00741021 ESI = 00744000
mov ecx,LENGTHOF
ECX = 00000005 EIP = 00741026
array
call ArraySum
mov theSum,eax
INVOKE ExitProcess,0
main ENDP
ArraySum PROC EIP = 00741037 ESP = 003FF9FC
push esi EIP = 00741038 ESP = 003FF9F8
push ecx EIP = 00741039 ESP = 003FF9F4
mov eax,0 EAX = 00000000 EIP = 0074103E
L1:
add eax,[esi] EAX = 00010000EIP = 00741040EFL = 00000206
add esi,TYPE DWORD ESI = 00744004EIP = 00741043EFL = 00000202PE = 0
loop L1 ECX = 00000004EIP = 0074103EEAX = 00030000EIP = 00741040EFL = 00000206
pop ecx ECX = 00000005EIP = 00741046ESP = 00F6FA34
pop esi ESI = 00744000EIP = 00741047 ESP = 00F6FA38
ret
ArraySum ENDP
END main
2. Define a procedure named “factorial_proc” that returns in EAX the factorial of a number saved in EBX. Use this
procedure to find factorial of each element in the array my_arr initialized as below and store each factorial at its
respective place in a new array arr_factorial.
my_arr WORD 1, 3, 5, 7, 9
6
Lab-4
Paste screenshot of code and Memory window showing final result here:
3. Write a program that inputs from user a string of maximum 20 characters. The prompt displayed on screen should be
as follows:
Please enter a string of maximum 20 characters:
The program then checks whether the string is a palindrome or not and outputs accordingly on DOS screen one of
the following strings:
Yes, a palindrome!
No, not a palindrome…
Paste screenshot of code and console window showing one instance of both cases (palindrome, not palindrome):
7
EE243L: Microcontrollers and Interfacing Lab
4. Write a procedure that returns a 0 in EBX if the remainder of the division of a numberkept in EAX register by a
number in any register of your choice (size should be 16 bits) is zero and returns 1 in EBX if the remainder is non-
zero. Paste here screenshot of code and Registers window showing final output:
8
Lab-4
9
EE243L: Microcontrollers and Interfacing Lab
10
Lab-4
11