0% found this document useful (0 votes)
21 views7 pages

Ece3212l Exer5 PDF

This document is a laboratory exercise focused on the concepts of stack and interrupts in assembly language. It explains the stack structure, assembly commands for pushing and popping data, and the syntax for generating interrupts, along with examples of DOS function codes. Additionally, it covers variable definitions and data types in assembly, along with exercises for practical application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

Ece3212l Exer5 PDF

This document is a laboratory exercise focused on the concepts of stack and interrupts in assembly language. It explains the stack structure, assembly commands for pushing and popping data, and the syntax for generating interrupts, along with examples of DOS function codes. Additionally, it covers variable definitions and data types in assembly, along with exercises for practical application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Stack and Interrupts

Laboratory Exercise #5

Objectives
• To be introduced to the concept of stack.
• To learnt he different assembly language commands involving the stack.
Discussion
WHAT IS A STACK?

• Stack is a one dimensional data structure which follows the Last In First Out (LIFO) principle. It is
an area of memory for keeping temporary data.

• The stack is a very important component of the CPU used for temporary storage of data and
addresses. Items are added and removed from the top of the structure called Top of the stack. A
familiar example is a stack of dishes; the last dish to go on the stack Is the top one, and it's the
only one that can be removed easily.

.STACK directive/command is used to reserve a block of memory for stack

e.g. .stack 100h (reserves 100 bytes for stack on memory)

• When program is assembled and loaded into memory:


o SS (Stack Segment) Register – holds stack Segment Address
o SP (Stack pointer) Register – initializes to the value specified with the .STACK directive; this
represents an empty stack position; when stack is not empty, SP represents top of the
stack
• Stack grows towards the beginning of the memory pointed by SS:SP

PUSH

• Used to add a new source (16-bit register or memory word e.g. AX) on stack
• Examples: push ax
push dx
push bl (not valid because not 16-bit)
• Results
o SP to decrement by2
o A copy of source contents is moved to the address specified by SS:SP
o Source remains unchanged
• PUSHF has no operand; it ushes the contents of FLAG register onto the stack

EXAMPLE: Take note of the Stack Pointer (SP) position.

Empty Stack After Push AX After Push BX

POP

• Used to remove top item from stack to a 16-bit register or memory word destination (e.g. AX)
• E.g. pop ax
pop bx
pop dl (invalid because not 16-bit)
• The contents of SS:SP (top of stack) is moved to the destination
• SP is incremented by2
• POPF has no operand and pops the top of the stack into FLAG register

EXAMPLE
Empty Stack After Pop CX After Pop DX
WHAT IS AN INTERRUPT?

The syntax for an assembly language instruction for x86 processors to generate an interrupt is:

INT x
where x is the software interrupt within the range 0 up to 255.

As customary with machine binary arithmetic, interrupt numbers are often written in hexadecimal form.

For instance, here are some INT 21h are DOS (Disk Operating System) Function Codes:

• AH=01h Reads Character from a standard input (e.g. keyboard) with echo ( displayed on screen)
Result: AL = character inputted

Note: Press Ctrl-C to break the input read

• AH=02h Writes Character to a standard input


Reqd: DL=character to write
Result: AL=laster character output

• AH=09h Prints a String


Reqd: DS:DX
- The string must be terminated by a ‘$’ character
- DS must point to the string’s segment while DX must contain the string’s offset
Result: AL=24h

To clear screen:
• mov ah, 00
mov al, 02
Int 10h

VARIABLES AND DATA TYPES IN ASSEMBLY

In Assembly language, there are no distinct data types like char,string,int,float,double,et. Instead, various
define assembler directives for reserving storage space are used:

The syntax for storage allocation statement for initialized data is:

[variable-name] define-directive initial-value

Examples:
answer DB ‘y’
no DW 12345
neg_no DW -12345
real_no DQ 123.456
There are 5 basic forms of the define directive:

Directive Purpose Storage Space


DB Define Byte Allocates 1 byte
DW Define Word Allocates 2 bytes
DD Define Doubleword Allocates 4 bytes
DQ Define Quadword Allocates 8 bytes
DT Define Ten Bytes Allocates 10 bytes

Directives are commands embedded in the source code that is recognized and acted upon by the assembler.
However, they do not execute at runtime like assembly instructions. Directives can define variables, macros and
procedures. They can also assign names to program sections or memory segments.

Examples of directives are:

.data

This identifies the area of the program(DS segment) containing variables

.code

This identifies the area of the program(CS segment) containing executable instructions.

.stack 100h

This reserves 100 bytes for stack (SS segment).

org 100h is also a compiler directive (it tells compiler how to handle the source code). This directive is very
important when you work with variables. It tells compiler that the executable file will be loaded at the offset of
100h (256 bytes), so compiler should calculate the correct address for all variables when it replaces the variable
names with their offsets. Directives are never converted to any real machine code.

Why executable file is loaded at offset of 100h? Operating system keeps some data about the program
in the first 256 bytes of the CS (code segment), such as command line parameters and etc. Though this is true
for COM files only, EXE files are loaded at offset of 0000, and generally use special segment for variables.

Exercises

Remark: In all instances, make a new .COM file in EMU8086. Each Assembly code is understood to start with
ORG 100h and ends with RET (returns control to the operating system):
STACK:

1. Run this code and perform single step. Complete the table of SP values after the specified instruction is
executed:

.stack 100h Instruction SP


Initial value FFFE
mov ax, 123h push ax FFFC
FFFA
mov bx, 789h
456h push bx
mov
push cx,
ax push cx FFF8
push bx pop ax FFFA
push cx pop bx
pop cx FFFC
pop ax FFFE
pop bx
pop cx Final values of the following registers:

0789 BX = 0456
AX = ____ 0123
_____ CX = _______

INTERRUPTS:

2. Let us try to read from the keyboard. Run this code. This will ask for an input from the keyboard. When
prompted, click the lowercase ‘a’. Run the code again. This time click capital ‘A’ when prompted.

mov ah,01h After running the code:


int 21h a) When ‘a’ was entered, AL = 61h
____
b) When ‘A’ was entered, AL= 41h
____

What do you notice of the output above? It should display the ASCII code of the character entered from the
keyboard.
3. Let us display a character on the monitor this time.

a)

mov ah,02h What is the output displayed on the monitor, and


mov dl, 41h why? The code displays the character A on the monitor because ASCII 41h
corresponds to A, and DOS interrupt 21h function 02h outputs the
int 21h character in register DL.
b)

mov ah,02h What is the output displayed on the monitor, and


mov dl, ‘a’ why? The output is a because DOS interrupt 21h function 02h
displays the character stored in register DL, which is the
int 21h ASCII value of 'a'.

4.

Make an assembly language program that will ask for a e.g.


single character from the keyboard then display the same aa
character on the screen. There must be 2 the same
characters on the display monitor. The first, is the e.g.
character entered from the keyboard with echo, and the AA
second, the duplicated character display.
e.g.
33

5.

Make an assembly language program that will display ASCII e.g.


characters starting from 41H up to 7Bh.
Hint: Use int 21h to display. Use add or inc to increment. ABCDEFGHIJKL…..
Use cmp and jne to make a loop.

6.

Revise your previous program so that the display will be e.g.


vertical. A
Hint: Use carriage return and line feed characters (like B
pressing the Enter key in MSWord). C

7. Run this code. Take note that a string to be printed must be terminated with a dollar sign ‘$’ to indicate
its end. Indentions in the code are optional.

.data What is the output?


msg: db "Hello, world! $”
.code
start: mov dx, offset msg ; load offset of msg into register DX
mov ah, 09h
int 21h ; print string to screen

You might also like