Assembly Language Programming Assignment Unit 7
Name: Aditi Singh
Course: CS 1105-01 – AY2026-T4
University: University of the People
Date: May 26, 2026
1. Assembly Language Program Using a Stack for String Reversal
Assembly language is a low-level programming language that provides direct access to computer
hardware, memory addresses, and CPU registers. In this assignment, a high-level data structure—
specifically a stack—is implemented in x86 assembly language (8086 processor architecture) to solve the
classic programming problem of reversing a string.
A stack is a linear data structure that operates on the Last In, First Out (LIFO) principle. This inherent
behavior makes it ideal for reversal tasks, as the last character pushed onto the stack becomes the first
character to be popped off.
Complete and Optimized Source Code
Code snippet
.MODEL SMALL
.STACK 100H
.DATA
STRING DB 'HELLO$' ; Original string terminated with '$'
REVERSED DB 6 DUP('$') ; Pre-filled buffer to store the reversed string
.CODE
MAIN PROC
; Initialize data segment registers
MOV AX, @DATA
MOV DS, AX
; Initialize Source Index (SI) to point to the start of the string
LEA SI, STRING
XOR CX, CX ; Clear CX register to use as a character counter
PUSH_LOOP:
MOV AL, [SI] ; Load character into AL register
CMP AL, '$' ; Check if the end of the string has been reached
JE POP_INIT ; If '$', exit the push loop
MOV AH, 00H ; Clear AH register to prevent pushing garbage values
PUSH AX ; Push the full 16-bit AX register onto the stack
INC CX ; Increment character count
INC SI ; Move to the next character in the original string
JMP PUSH_LOOP ; Repeat for the next character
POP_INIT:
; Initialize Destination Index (DI) to point to the start of the REVERSED buffer
LEA DI, REVERSED
JCXZ DISPLAY ; Security check: If string is empty (CX=0), skip to display
REVERSE_LOOP:
POP AX ; Pop the 16-bit value from the top of the stack
MOV [DI], AL ; Store the lower byte (character) into REVERSED string
INC DI ; Move to the next memory address in the destination buffer
LOOP REVERSE_LOOP ; Automatically decrements CX and loops until CX = 0
DISPLAY:
; DOS interrupt to display the reversed string (Function 09H)
MOV AH, 09H
LEA DX, REVERSED
INT 21H
; DOS interrupt to safely terminate the program (Function 4CH)
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
2. Design and Development Process
The development process began by conceptualizing how a LIFO structure could efficiently manipulate
sequential array data. A stack data structure was chosen because its hardware-level pointer manipulation
naturally matches the algorithmic logic required for sequence inversion. Instead of shifting array elements
through nested loops—which incurs high computational overhead in high-level languages—the stack
pointer handles allocation and indexing dynamically.
Visual Representation of Stack Behavior
To understand the underlying mechanics of how data flows through the processor during execution,
consider the diagram below illustrating the transition of the string "HELLO":
Plaintext
[ INPUT STRING: "HELLO$" ]
1. PUSH PHASE (Looping SI) 2. POP PHASE (Looping DI via CX)
| 'O' | <-- Stack Top (SP) | |
| 'L' | | 'L' | --> Writes to REVERSED[0] = 'O'
| 'L' | | 'L' | --> Writes to REVERSED[1] = 'L'
| 'E' | | 'E' | --> Writes to REVERSED[2] = 'L'
| 'H' | <-- Stack Base | 'H' | --> Writes to REVERSED[3] = 'E'
----------- ----------- Writes to REVERSED[4] = 'H'
[ OUTPUT STRING (REVERSED): "OLLEH$" ]
Low-Level Concept Enhancement
Developing this program significantly enhances the understanding of low-level software execution and
hardware interactions in several ways:
● Memory Segmentation and Addressing: The program requires explicit initialization of the Data
Segment (DS) register using the @DATA directive. It establishes a practical grasp of indirect
addressing using index registers (SIand DI) to traverse sequential offsets in memory blocks.
● Register Management and Cleanliness: Unlike compilers that hide hardware intricacies, low-
level programming demands strict register hygiene. For example, because the 8086 architecture
does not support pushing an 8-bit register (AL), the whole 16-bit register (AX) must be used.
Clearing AH (MOV AH, 00H) is essential to prevent system garbage values from corrupting
stack frames.
● Flow Control and Loop Execution: Utilizing the LOOP instruction demonstrates how the CPU
relies on implicit flag updates and structural register dependencies. The execution relies directly
on the CX counter register to track stack bounds dynamically, which prevents critical logic
runtime bugs like infinite loops or stack underflow.
3. Benefits of Assembly Language for High-Level Structures
Implementing standard data structures (such as stacks, queues, or linked lists) in assembly language
provides distinct, quantifiable advantages over high-level environments:
Direct Resource and Hardware Control
In high-level languages like Java or Python, abstracting a stack requires initializing full objects or
dynamic vectors that carry extensive system overhead. Assembly language allows direct, clock-cycle-
accurate manipulation of the Stack Pointer (SP) register. Instructions like PUSH and POP execute nearly
instantaneously at the hardware level because they directly decrement and increment the physical
hardware pointer without software translation layers.
Optimized Memory Management and Zero Overhead
In embedded systems, internet-of-things (IoT) microcontrollers, and real-time kernels, memory space is
highly constrained. High-level languages compile code with bloated runtime environments, automated
garbage collection routines, and generic compiler metadata. Assembly language enables the programmer
to allocate precisely what is required—such as the exact bytes defined by DUP('$') arrays—preventing
memory fragmentation and maximizing deterministic runtime execution speed.
Low-Level Architecture Awareness
Building structural elements from scratch at the register level allows developers to understand exactly
how core operating systems manage execution processes. For instance, the precise stack mechanism used
in this string reversal program is identical to how modern operating systems handle CPU interrupt service
routines (ISRs), pass parameters during system-level function calls, and preserve CPU state contexts
during multitasking switches.
References
Intel Corporation. (2024). Intel 64 and IA-32 architectures software developer’s manual. Intel Developer
Documentation.
Irvine, K. R. (2020). Assembly language for x86 processors (8th ed.). Pearson.
Patterson, D. A., & Hennessy, J. L. (2017). Computer organization and design: The hardware/software
interface (5th ed.). Morgan Kaufmann.