[go: up one dir, main page]

0% found this document useful (0 votes)
17 views10 pages

COAL Lab Manual - Week 13

The document is a lab manual for a course on Assembly Programming at Ripah International University, focusing on the concept of the stack in assembly language. It explains stack operations, including PUSH and POP instructions, and provides example programs demonstrating stack usage and character input/output. Additionally, it outlines lab tasks for students to implement stack operations in their assembly programs.

Uploaded by

nazirkinzyy
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)
17 views10 pages

COAL Lab Manual - Week 13

The document is a lab manual for a course on Assembly Programming at Ripah International University, focusing on the concept of the stack in assembly language. It explains stack operations, including PUSH and POP instructions, and provides example programs demonstrating stack usage and character input/output. Additionally, it outlines lab tasks for students to implement stack operations in their assembly programs.

Uploaded by

nazirkinzyy
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/ 10

RIPHAH INTERNATIONAL UNIVERSITY, LAHORE

Computer Organization and Assembly Language


Lab
Spring-2025

Lab 13 Manual
Assembly Programming
Stack

Instructor: Engr. Amna Bibi


Office: Cabin outside Faculty Offices
Physics & Math 3rd Floor, Block A
Office Hours: Thursday 10:00-12:00
Lab # 10
Assembly Language Stack Implementation

Objective:
Concept of the stack in assembly language

Theory

Stack is one dimensional data structure. Items are added and removed from one end of the
structure; that is, it is processed in a “last in first out” manner. The most recent addition to the
stack is called the top of the stack.

How Stack Works (Simple Example 1):


mov ax, 1234h

push ax ; store AX on stack

mov ax, 5678h

push ax ; store another value

pop bx ; retrieve 5678h into BX

pop cx ; retrieve 1234h into CX

Memory Flow:

1. First 1234h is pushed.


2. Then 5678h is pushed.
3. First POP retrieves 5678h into BX.
4. Second POP retrieves 1234h into CX.

Real Usage of Stack:

• Saving CPU registers before interrupt/subroutine


• Local variable storage in recursive functions
• Handling nested function calls

Lab 10 2-6-25 Page |1


Basic Stack Instructions:
Instruction Meaning
PUSH reg Places the value of a register on the stack
POP reg Retrieves the last value from the stack into the register
CALL Pushes return address, jumps to subroutine
RET Pops return address and jumps back

PUSH
To add a new word to stack we PUSH it on. The syntax is

PUSH source

Where source is a16-bit register or memory word.


For example: PUSH AX.

POP
To remove the top item from the stack, we POP it. The syntax is

POP destination

Where destination is a 16-bit register (except IP) or memory word.


For example: POP DX

CALL (Call Procedure)

• The CALL instruction is used to jump to a subroutine (procedure).


• Before jumping, it saves the return address (the address of the instruction after the
CALL) onto the stack.
• This allows the program to return to the correct place after the subroutine finishes.

Lab 10 2-6-25 Page |2


What happens during CALL:

1. The current Instruction Pointer (IP) (next instruction address) is pushed onto the
stack.
2. Control is transferred to the address (label) specified by the CALL

RET (Return from Procedure)

• The RET instruction is used to return from a subroutine to the address that was saved
on the stack by the corresponding CALL.
• It pops the return address from the stack and jumps back to that address.

What happens during RET:

1. The top value from the stack (which is the return address) is popped into the IP.
2. Execution continues from where the CALL left off.

Example Program 2:
.model small
.stack 100h
.data
msg1 db 'Stack Demo Done!$'
.code
main:
mov ax, @data
mov ds, ax

; Step 1: Load values into registers


mov ax, 1111h
mov bx, 2222h

; Step 2: PUSH values onto stack


push ax ; Stack now has 1111h
push bx ; Stack now has 2222h on top of 1111h

Lab 10 2-6-25 Page |3


; Step 3: POP values into other registers
pop cx ; Should get 2222h (last in, first out)
pop dx ; Should get 1111h

; Step 4: Optional - Display message to confirm completion


lea dx, msg1
mov ah, 09h
int 21h

mov ah, 4ch


int 21h
end main

Purpose of SI and DI Registers


Register Name Purpose
SI Source Index Points to source data in memory (used for reading from an
array).
DI Destination Points to destination data in memory (used for writing to
Index memory).

Example Program 3:
Write a program that read a sequence of characters and display them in reverse order on the
next line.

.model small
.stack 100h
.data
msg1 db 'Enter a string (max 20 chars): $'
msg2 db 13,10,'Reversed string: $'
input db 20 dup(?) ; input buffer

Lab 10 2-6-25 Page |4


length db ? ; to store input length (max 20)
.code
main:
mov ax, @data
mov ds, ax

; Show input message


lea dx, msg1
mov ah, 09h
int 21h

; Read characters one by one


mov si, 0

read_loop:
mov ah, 01h ; read character
int 21h
cmp al, 13 ; if Enter pressed
je input_done
mov input[si], al ; store char in buffer
inc si
cmp si, 20 ; limit input to 20 chars
je input_done
jmp read_loop

input_done:
; move si to cx safely and store to length
mov cx, si ; store character count
mov length, cl ; use CL (lower 8 bits) for byte var

Lab 10 2-6-25 Page |5


; Show output message
lea dx, msg2
mov ah, 09h
int 21h

; Print in reverse
mov cl, length ; get count in CL
mov si, cx
dec si ; point to last character

print_loop:
mov dl, input[si]
mov ah, 02h
int 21h
dec si
loop print_loop

; Exit program
mov ah, 4ch
int 21h
end main

Code Explanation:
Part Function
.data section Stores messages, input array, and length
Input loop Reads characters one by one
input[si] Stores each character
Reverse print Uses SI and loop to go backward
int 21h DOS service to handle input/output

Lab 10 2-6-25 Page |6


Setup Section

• .model small
→ Use small memory model (separate <64KB code/data segments).
• .stack 100h
→ Reserve 256 bytes for the stack.
• .data
→ Start of the data segment (stores messages and variables).

Data Declarations

• msg1
→ Message: "Enter a string (max 20 chars):", ends with $ for int 21h.
• msg2
→ Message to show before the reversed string (\nReversed string:).
• input db 20 dup(?)
→ Declare a 20-byte array to store input characters.
• length db ?
→ Variable to store number of entered characters.

Program Start

• main:
→ Entry point of the code.
• mov ax, @data → Load address of data segment into AX.
mov ds, ax → Set DS to point to data for accessing variables.

Display Input Prompt

• Load msg1 into dx,


call int 21h / ah = 09h → display message to enter input.

Input Loop

• mov ah, 01h → Read one character from keyboard using int 21h.

Lab 10 2-6-25 Page |7


• cmp al, 13 → Check if ENTER (carriage return) is pressed.
je input_done → If yes, jump to input-done label.
• mov input[si], al → Store the character at current index.
inc si → Move to next index.
• cmp si, 20 → Limit input to 20 characters.
je input_done → Stop if max length reached.
• jmp read_loop → Repeat input loop.

Store Input Length

• mov cx, si → Store total number of characters into CX.


• mov length, cl → Store length into the length variable.

Display Reverse Message

• Load msg2 into dx,


use int 21h / ah = 09h → Print "Reversed string:" on new line.

Print String in Reverse

• mov cl, length → Load number of characters to reverse.


• mov si, cx → Set SI = length.
dec si → Move to the last character's position.
• print_loop:
o mov dl, input[si] → Load character from array in reverse.
o mov ah, 02h → Use int 21h to display character.
o dec si → Move backward.
o loop print_loop → Repeat until all characters printed.

Program Exit

• mov ah, 4ch


int 21h
→ Exit the program gracefully.

Lab 10 2-6-25 Page |8


Lab Tasks

Write an assembly program in EMU8086 to:

1. Read two 8-bit numbers from the user.


2. Push both numbers onto the stack.
3. Pop them back into two different registers.
4. Add the two numbers and display the result.

Lab 10 2-6-25 Page |9

You might also like