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

Chapter Seven Ass

Chapter seven assignment for computer design and architecture

Uploaded by

Gatwech Gatjiek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Chapter Seven Ass

Chapter seven assignment for computer design and architecture

Uploaded by

Gatwech Gatjiek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Chapter seven assignment

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

solidifying my understanding of low-level programming concepts.

1. Assembly Language Program (String Reversal using a Stack):

For this project, I chose to use the x86-64 assembly language, specifically targeting a Linux

environment. The program does the following:

• Initialization:

• Defines a memory region for the stack.

• Allocates space to store the input string and the reversed string.

• Input:

• Prompts the user to enter a string.

• 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:

• Prints the reversed string.

• Termination: Exits the program.

Here is a simplified version of the program to better describe the process (I am not putting in

the full program due to its length):

section .data

prompt db "Enter a string: ", 0

output db "Reversed string: ", 0

input_buffer resb 256 ; Reserve 256 bytes for input string

reversed_buffer resb 256 ; Reserve 256 bytes for reversed string

stack resb 256 ; Reserve 256 bytes for the stack

stack_top dq 0 ; this variable will store the address of the current top of the stack
section .bss

section .text

global _start

_start:

; Prompt user for input

mov rax, 1 ; syscall number for write

mov rdi, 1 ; file descriptor 1 (stdout)

mov rsi, prompt ; message address

mov rdx, 16 ; message length

syscall

; Read input

mov rax, 0 ; syscall number for read

mov rdi, 0 ; file descriptor 0 (stdin)

mov rsi, input_buffer ; memory address of input

mov rdx, 255 ; max bytes to read

syscall
mov rdi, stack

mov [stack_top], rdi

; --- PUSH string to stack --

push_loop:

movzx rbx, byte[rsi] ; get character from input

cmp rbx, 10 ; check if it is newline

je pop_loop ; if it is newline go to pop_loop

push rbx ; PUSH char onto stack

inc rsi

jmp push_loop

;--- POP string from stack --

pop_loop:

cmp rdi, stack

je end_prog ; Check if stack is empty

pop rbx

mov byte [reversed_buffer], bl

inc rdi
jmp pop_loop

end_prog:

; Output the reversed string

mov rax, 1

mov rdi, 1

mov rsi, output

mov rdx, 17 ; length of 'Reversed string:'

syscall

mov rax, 1

mov rdi, 1

mov rsi, reversed_buffer

mov rdx, 256 ; max length of the string

syscall

; Exit the program

mov rax, 60 ; syscall for exit

xor rdi, rdi


syscall

2. Design and Development Process:

My process involved these key steps:

 • Understanding the Problem: First, I carefully understood the string reversal problem

and the LIFO (Last-In-First-Out) nature of a stack.

 • 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.

 • Assembly Language Implementation: I translated the high-level algorithm into x86-64

assembly language. This required careful consideration of registers, stack operations

(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

popping the values off in the right order.


 • Optimization: I continuously looked for ways to make the code more efficient (e.g., by

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.

How this project enhanced my understanding:

• Memory Management: I gained a deep understanding of how memory is used in a program

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

register usage, data movement, and control flow instructions.

• Low-Level Abstraction: I learned about system calls and how programs interact with the

operating system at a very low level.

• Debugging Skills: The process forced me to become proficient in using debuggers, which is a

core skill for any programmer.

• Data Representation: The project required me to carefully consider how data (characters) is

stored in memory and manipulated in assembly.


3. Benefits of Assembly Language for High-Level Data Structures:

While assembly is generally not used for implementing entire high-level data structures, this

project highlighted some unique benefits:

• Low-Level Control: Assembly allows for maximum control over memory allocation and data

manipulation. This can be essential for resource-constrained environments or in highly

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.

• Performance Optimization: By having a better understanding of assembly it is possible to

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

therefore I had to understand how they worked at a very low level.


• Understanding Computer Architecture: Implementing data structures in assembly forces a

deeper understanding of how the computer's hardware (CPU, memory) works. This gave me a

deeper understanding of what happens in memory during runtime.

• 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:

• Embedded Systems: In embedded systems or microcontrollers with limited resources, hand

-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.

• Cryptographic Algorithms: High-performance cryptographic routines are sometimes

implemented in assembly for efficiency.


In conclusion, while not a practical choice for most day-to-day programming, implementing a

stack in assembly has been a highly beneficial learning experience for me. It has deepened my

understanding of computer architecture, memory management, and the foundations of high-

level programming languages. I look forward to continuing my exploration of assembly

language!

Reference

Anil K. Maini. (2007).Digital Electronics: Principles, Devices and Applications

You might also like