/Dandamudi-Assembly-Solutions

Solutions for programming exercises of "Introduction to Assembly Language Programming (Second Edition)" by Sivarama P. Dandamudi

Primary LanguageAssemblyMIT LicenseMIT

📖 About

Solutions for programming exercises of Introduction to Assembly Language Programming (Second Edition) by Sivarama P. Dandamudi

❔ Problems

🔖 Chapter 4 🔖 Chapter 5 🔖 Chapter 6 🔖 Chapter 7 🔖 Chapter 8 🔖 Chapter 9 🔖 Chapter 10 🔖 Chapter 11 🔖 Chapter 14
No. Question Code
1 Q04-01 P04-01
2 Q04-02 P04-02
3 Q04-03 P04-03
4 Q04-04 P04-04
5 Q04-05 P04-05
6 Q04-06 P04-06
7 Q04-07 P04-07
8 Q04-08 P04-08
9 Q04-09 P04-09
No. Question Code
1 Q05-01 P05-01
2 Q05-02 P05-02
3 Q05-03 P05-03
4 Q05-04 P05-04
5 Q05-05 P05-05
6 Q05-06 P05-06
7 Q05-07 P05-07
8 Q05-08 P05-08
9 Q05-09 P05-09
10 Q05-10 P05-10
11 Q05-11 P05-11
12 Q05-12 P05-12
No. Question Code
1 Q06-01 P06-01
2 Q06-02 P06-02
3 Q06-03 P06-03
4 Q06-04 P06-04
5 Q06-05 P06-05
6 Q06-06 P06-06
7 Q06-07 P06-07
8 Q06-08 P06-08
9 Q06-09 P06-09
10 Q06-10 P06-10
11 Q06-11 P06-11
12 Q06-12 P06-12
13 Q06-13 P06-13
No. Question Code
1 Q07-01 P07-01
2 Q07-02 P07-02
3 Q07-03 P07-03
4 Q07-04 P07-04
5 Q07-05 P07-05
6 Q07-06 P07-06
7 Q07-07 P07-07
8 Q07-08 P07-08
9 Q07-09 P07-09
10 Q07-10 P07-10
11 Q07-11 P07-11
No. Question Code
1 Q08-01 P08-01
2 Q08-02 P08-02
3 Q08-03 P08-03
4 Q08-04 P08-04
5 Q08-05 P08-05
6 Q08-06 P08-06
7 Q08-07 P08-07
No. Question Code
1 Q09-01 P09-01
2 Q09-02 P09-02
3 Q09-03 P09-03
4 Q09-04 P09-04
5 Q09-05 P09-05
6 Q09-06 P09-06
7 Q09-07 P09-07
8 Q09-08 P09-08
9 Q09-09 P09-09
No. Question Code
1 Q10-01 P10-01
2 Q10-02 P10-02
3 Q10-03 P10-03
4 Q10-04 P10-04
5 Q10-05 P10-05
6 Q10-06 P10-06
7 Q10-07 P10-07
8 Q10-08 P10-08
9 Q10-09 P10-09
10 Q10-10 P10-10
11 Q10-11 P10-11
No. Question Code
1 Q11-01 P11-01
2 Q11-02 P11-02
3 Q11-03 P11-03
4 Q11-04 P11-04
5 Q11-05 P11-05
6 Q11-06 P11-06
7 Q11-07 P11-07
8 Q11-08 P11-08
9 Q11-09 P11-09
No. Question Code
1 Q14-01 P14-01
2 Q14-02 P14-02
3 Q14-03 P14-03
4 Q14-04 P14-04
5 Q14-05 P14-05

ℹ️ How-to

To be able to run the codes in this repository, you need to be under a Linux environment. there are three solutions:

  1. Linux as main OS or as a dual boot with another OS
  2. Linux in a virtual machine
  3. WSL (Windows Subsystem for Linux)

The first two are quite simple and don't need any explanation, but what is WSL?

WSL

Windows Subsystem for Linux is a compatibility layer for running Linux binary executables natively on Windows. Different distros can be installed as WSL, but the preferred distro is Ubuntu 18.04 LTS which you can get from Microsoft store. A detailed guide for installing WSL can be found here. After installing and enabling WSL, we need to allow the support for 32-bit binaries in WSL. A brief explanation on how to do this can be found in a GitHub issue comment here.

🔧 Tools

The next step is to download and install a couple of tools that we are going to need. The first tool is a debugger. The debugger of choice for WSL users is the x64dbg which can be installed on windows from here, And for others, the edb-debugger is a good option. The edb-debugger can be installed on Ubuntu by typing the following line in the terminal:

sudo apt install edb-debugger 

The other tool that we're going to need is NASM (Netwide Assembler). NASM can be installed on Ubuntu by typing the following line in the terminal:

sudo apt install nasm

⌨️ Code

Now that we have prepared the environment for writing assembly code, it's good to also have a quick look at how x86 assembly language looks. Generally, a typical x86 source code that ends with the ".asm" extension, is made up of different sections, mainly .data, .bss, and .code (or .text). Each section contains a number of lines that are each made of either an instruction with its arguments, a label, or both. the .code section has to start with a global label called _start and for exiting the program, we use the following code fragment for WSL:

push 0
call ExitProcess

and for Linux in other environments (VM, etc) we use the following fragment:

mov eax, 1
mov ebx, 0
int 0x80

You will later on understand these code fragments perfectly fine; so for now, just remember that this exit method is the main difference between the code written under WSL and the code written under other Linux environments.

⌨️ Write Programs

In order to write programs in x86 NASM assembly, we need to use the tools provided in Dandamudi-Assembly-Solutions/Tools/ so the easier approach is to write our programs inside either the Linux or the WSL folder.

WSL

There are 4 files in the WSL folder that help us write code in x86 assembly.

  • lib.h
  • libw.obj
  • GoLink.exe
  • asmw

lib.h

Most of the code we write in this book includes the file named "lib.h" at the beginning. This is a header file that contains a set of macros for input/output that are supposed to make your job easier. just like in C, if we want to use the stuff defined in a header file, we need to include it in our source file. the syntax for including a file in x86 assembly is %include [filename]

libw.obj

libw.obj is a set of functions written and assembled into an object file to be used and linked with your program, to give you some utility to work with. lib.h defines macros for the functions in libw.obj, so these files are used together in our programs.

GoLink.exe

The process of writing, assembling and running a program written in x86 assembly is pretty simple. First, our file is assembled with an assembler (NASM) into an object file. Then, this object file is linked with other object files that we use (libw.obj) and together they create a 32 bit ELF binary executable file. the process of linking the object files in WSL is done using GoLink.exe.

asmw

A Bash script has been provided for you that handles all the steps of assembling and linking an assembly source file, and executes the resulting binary file in the end; this file is called asmw for WSL. We use asmw by providing it our source file as an argument; so inside our bash window open, we need to write ./asmw [filename] in order for it to work, where filename is an assembly source file that ends with ".asm".

Linux

There are 3 files in the Linux folder that help us write code in x86 assembly.

  • lib.h
  • lib.o
  • asmw

lib.h

Most of the code we write in this book includes the file named "lib.h" at the beginning. This is a header file that contains a set of macros for input/output that are supposed to make your job easier. just like in C, if we want to use the stuff defined in a header file, we need to include it in our source file. the syntax for including a file in x86 assembly is %include [filename]

lib.o

lib.o is a set of functions written and assembled into an object file to be used and linked with your program, to give you some utility to work with. lib.h defines macros for the functions in lib.o, so these files are used together in our programs.

asm

The process of writing, assembling and running a program written in x86 assembly is pretty simple. First, our file is assembled with an assembler (NASM) into an object file. Then, this object file is linked with other object files that we use (lib.o) and together they create a 32 bit ELF binary executable file. A Bash script has been provided for you that handles all the steps of assembling and linking an assembly source file, and executes the resulting binary file in the end; this file is called "asm" for Linux. We use asm by providing it our source file as an argument; so inside our bash window open, we need to write ./asm [filename] in order for it to work, where filename is an assembly source file that ends with ".asm".

⚠️ NOTE: before using asm for the first time, we need to make it executable by typing in the following line in bash:

chmod +x asm

asm/asmw

The bash scripts asm/asmw are just a sequence of bash commands that are supposed to make your job easier so you don't have to execute all the commands every time you want to assemble a program. If you read the scripts you can easily understand what's going on and then you can customize the scripts to fit your own purposes. (e.g. change the pathnames in the script so you can assemble and run your programs from outside the folders.)

🤝 Contributing

To contribute to this project, please follow the steps below:

  1. fork the repository
  2. make all the changes that you want to see in the original repository (if you want to add or change some solutions, don't forget to follow assembly coding standards in your codes)
  3. push your changes to a new branch in your fork and create a pull request along with changes explanation

©️ License

See LICENSE.