Chapter Seven Ass
Chapter Seven Ass
This report details my experience creating a simple assembly language program to implement a
stack data structure, which I then used to reverse a string. This project has been invaluable in
For this project, I chose to use the x86-64 assembly language, specifically targeting a Linux
• Initialization:
• Allocates space to store the input string and the reversed string.
• Input:
• Reads the string from the user into the input buffer.
• String Reversal (using Stack):
• Push onto Stack: Iterates through the input string, pushing each character onto the stack.
The stack pointer (RSP) is used to manage the memory address of the stack and is decremented
as items are pushed onto the stack. The stack grows downward towards lower memory
addresses.
• Pop from Stack: Iterates again, popping each character from the stack into the reversed
string buffer. The stack pointer (RSP) is incremented as items are popped off the stack.
• Output:
Here is a simplified version of the program to better describe the process (I am not putting in
section .data
stack_top dq 0 ; this variable will store the address of the current top of the stack
section .bss
section .text
global _start
_start:
syscall
; Read input
syscall
mov rdi, stack
push_loop:
inc rsi
jmp push_loop
pop_loop:
pop rbx
inc rdi
jmp pop_loop
end_prog:
mov rax, 1
mov rdi, 1
syscall
mov rax, 1
mov rdi, 1
syscall
• Understanding the Problem: First, I carefully understood the string reversal problem
• Algorithm Design: I devised a clear algorithm that uses the stack to reverse the string.
This algorithm consisted of pushing each character of the input string onto the stack,
and then popping each character from the stack and putting it into the new reversed
string.
(PUSH and POP), memory addresses, and syscalls (system calls) for I/O operations.
• Testing and Debugging: I compiled the program using an assembler (like NASM) and
used a debugger (like GDB) to identify and fix any errors, such as segmentation faults or
incorrect logic. I carefully checked that the values at each step of the program were as
they were supposed to be to make sure I was pushing the right values to the stack, and
reducing the number of instructions or memory accesses). I also worked to remove code
repetition, and made sure the code was clean and easy to read.
and how the stack pointer (RSP) works, and how stack operations directly manipulate the
system memory.
• Instruction Set Architecture: I became more familiar with the x86-64 instruction set, including
• Low-Level Abstraction: I learned about system calls and how programs interact with the
• Debugging Skills: The process forced me to become proficient in using debuggers, which is a
• Data Representation: The project required me to carefully consider how data (characters) is
While assembly is generally not used for implementing entire high-level data structures, this
• Low-Level Control: Assembly allows for maximum control over memory allocation and data
optimized systems. I learned that it was important to reserve sufficient space for the input
string and the stack. I also saw that it was crucial to track where the current top of the stack
was via a variable so I could use that value later to pop the correct number of items off the
stack.
write very optimized code where you can finely tune every little part of the program to run as
efficiently as possible. The PUSH and POP operations were the central parts of the program, and
deeper understanding of how the computer's hardware (CPU, memory) works. This gave me a
• Better Appreciation of Higher-Level Languages: Having gone through the process of doing this
in assembly has made me have more of an appreciation for higher level languages like Python
and Java, because these processes are all automated in higher level programming.
• Learning how a Stack works: Learning how to build a stack in assembly provided me with the
core knowledge of how they work and why they are used.
Relevant Examples:
-written assembly can be necessary for optimizing code performance and minimizing memory
usage.
• Operating Systems: Parts of the operating system kernel and device drivers might be written
in assembly for very low-level operations or critical sections where performance is paramount.
stack in assembly has been a highly beneficial learning experience for me. It has deepened my
language!
Reference