0% found this document useful (0 votes)
4 views6 pages

Assembly Assignment

This document is a group assignment on Microprocessor and Assembly Language Programming, prepared by three students and submitted to a teacher. It includes definitions of key terms related to stack operations, an analysis of assembly code, and a programming task to reverse three numbers using a stack. The document also illustrates the importance of matching PUSH and POP operations to maintain stack integrity.

Uploaded by

Alemseged Mammo
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)
4 views6 pages

Assembly Assignment

This document is a group assignment on Microprocessor and Assembly Language Programming, prepared by three students and submitted to a teacher. It includes definitions of key terms related to stack operations, an analysis of assembly code, and a programming task to reverse three numbers using a stack. The document also illustrates the importance of matching PUSH and POP operations to maintain stack integrity.

Uploaded by

Alemseged Mammo
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

1

Department of Computer Science

Microprocessor and Assembly Language Programing

Group Assignment

Prepared By
Alemseged Mammo ECS/1416/16

Dawit Aklilu ECS/1517/16

Alemnesh Zewdu ECS/1828/16

Submitted To: - Mrs. Getaneh


2

Short Answer

1. Define the following terms:


 Stack
A stack is a reserved area in memory used to temporarily store data during program
execution. It grows and shrinks dynamically.
 LIFO
Stands for Last In, First Out. This means the last item pushed onto the stack is the first
one popped off.
 PUSH instruction
PUSH places a value (from a register or memory) onto the top of the stack and
decrements the stack pointer.
 POP instruction
POP removes the top value from the stack into a destination (register or memory) and
increments the stack pointer.
2. List all registers that can be used as source/destination in PUSH/POP instructions.
 16-bit real mode (x86):
AX, BX, CX, DX, SI, DI, BP, SP, DS, ES, SS, CS
 32-bit protected mode:
EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP
 Segment registers:
CS, DS, ES, SS (some restrictions on CS for POP)
3. Explain why it's important to have an equal number of PUSH and POP operations.

Having an equal number of PUSH and POP ensures the stack pointer returns to its original position after
operations. If there are more PUSHes than POPs, the stack pointer drifts, causing memory leaks or
incorrect returns. If more POPs occur, the program may read invalid stack data, leading to crashes or
corrupting return addresses.

4. Analyze the following code:

assembly

ORG 100h

MOV AX, 1234h

PUSH AX

MOV BX, 5678h

PUSH BX

POP AX

POP BX

RET
3

a) Final values in AX and BX:

 After first PUSH: Stack has 1234h


 After second PUSH: Stack has 5678h (top)
 POP AX → AX = 5678h
 POP BX → BX = 1234h

Final:
AX = 5678h, BX = 1234h

b) Stack contents after each operation:

Step Operation Stack Top → Bottom

Start - [empty]

After PUSH AX Push 1234h [1234h]

After PUSH BX Push 5678h [5678h, 1234h]

After POP AX Pop 5678h to AX [1234h]

After POP BX Pop 1234h to BX [empty]

c) If only one POP instead of two:

The stack would still contain 1234h at the end. The RET instruction would use the wrong return address
(1234h instead of the correct one), causing the program to crash or jump to an invalid location.

5. Output of the following program:

assembly

ORG 100h

MOV AX, 1111h

MOV BX, 2222h

PUSH AX

PUSH BX
4

POP AX

POP BX

RET

Explanation:

 Push AX (1111h)
 Push BX (2222h)
 Pop to AX → AX = 2222h
 Pop to BX → BX = 1111h

Output: The registers are swapped:


AX = 2222h, BX = 1111h
(No direct screen output unless displayed with interrupt).

Programming

6. Assembly program to reverse three numbers using stack:

assembly

ORG 100h

; Prompt user

MOV DX, OFFSET msg1

MOV AH, 09h

INT 21h

; Input first digit

MOV AH, 01h

INT 21h

SUB AL, 30h ; convert ASCII to number

PUSH AX

; Prompt again
5

MOV DX, OFFSET msg2

MOV AH, 09h

INT 21h

; Input second digit

MOV AH, 01h

INT 21h

SUB AL, 30h

PUSH AX

; Prompt again

MOV DX, OFFSET msg3

MOV AH, 09h

INT 21h

; Input third digit

MOV AH, 01h

INT 21h

SUB AL, 30h

PUSH AX

; Now pop in reverse order

POP DX ; third digit

POP CX ; second digit

POP BX ; first digit


6

; Display reversed

MOV AH, 02h

MOV DL, BH ; assuming small numbers <10, BX holds first digit

ADD DL, 30h

INT 21h

MOV DL, CH ; second digit

ADD DL, 30h

INT 21h

MOV DL, DH ; third digit

ADD DL, 30h

INT 21h

RET

msg1 DB 'Enter first digit: $'

msg2 DB 0Dh,0Ah,'Enter second digit: $'

msg3 DB 0Dh,0Ah,'Enter third digit: $'

You might also like