Welcome to the comprehensive Assembly Language Programming (ALP) course. I will teach you the ALP with the oldest Intel architecture iAPX88 also known as 8088 and iAPX386, but we will more focus on the iAPX88 concepts and then you can translate these fundamentals concept into any architecture.
Please while you are reading this article content, please keep in mind that focus on the concept instead of making excuses like it’s the oldest architecture and I want to learn x86 programming. After learning ALP with iAPX88 you can be able to translate these concepts in any Intel architecture by reading their documentation.
Before beginning I want to tell you that article will update day by day and I’ll add more and more content to this, so bookmark this article and let’s get started.
Assembly Language Programming Contents
Setting Up ALP Environment
To get started with Assembly language we need to set up the environment where we can write and run the ASM code. Just follow the following steps to set it up:
- Download the VS Code and install it by checking all the options.
- Now download the ASM Setup from the GitHub, just click the green ‘Code’ button and you will see the ‘Download ZIP’ option.
- Go to your Downloads folder and extract the download zip file.
- In the extracted folder you will see the ‘DOSBox Software’ folder. Open it and install the DOSBox.
- After that move the ‘ASM’ folder to your C drive.
- Now open the folder in the VS Code and you will see the ‘ismail-code.asm’ file in the left side of the VS Code. Just open it up and you will see the starter code.
So, now we have to check is our ALP environment is properly setup we have to run the Assembly language code.
Open the DOSBox software and execute the following commands one by one:
mount x c:/asm
x:
nasm ismail-code.asm -o ismail-code.com -l ismail-code.lst
afd ismail-code.com
If you didn’t see the error then it’s good to get started, otherwise follow the same steps as mentioned above.
Our First Assembly Language Program
Before writing the Assembly language programs we have to understand some basic things.
The assembly language program file is always having the .asm extension. So, whenever you create a file you have to write .asm after your file name. One suggestion is to you that you write your file name in lowercase separated by dash (-) symbol.
Syntax
The basic syntax of assembly language for iAPX88 process is the following:
[org 0x0100]
; body of ASM program
mov ax, 0x4c00
int 0x21
- Our first line in the ASM file would always be [org 0x0100]. It gives directive to DOS o originate or start the next line of the code at address 0x0100. The 0x denotes the value is hexadecimal.
- Now after this our program body will by started. In this we will write our assembly instructions.
- At the last of the file, we have two instructions. The mov ax, 0x4c00 instruction is used to terminate the program, and the int 0x21 instruction is and interrupt call to operating system i.e. what the OS should do for me.
Instruction Format
The assembly instructions format start with operation, then destination, and then source. You can have source or destination omitted, and also both the source, destination can be omitted in some instructions.
- operation destination, source
- operation destination
- operation source
- operation
Do we write the assembly code in Binary (0, 1)?
No, we don’t write the binary code in assembly language, instead in assembly language there is a set of abbreviated English like words i.e. Instruction Mnemonics, that help us to write our code. Then we give it to Assembler that assemble the program, in our case it’s the Netwide Assembler (NASM).
In assembly language there are many instructions; to get stared we will see some basic instruction and we will use these in our assembly programs. You also note that these instructions are reserved keywords.
Assembly Mnemonics/Instructions
This is the list of basic iAPX88 instruction set. You can use the instruction in lower or upper case in your program, buy usually recommended to use lower case.
Action | Abbreviation | Action | Abbreviation |
---|---|---|---|
Comment | ; | Load | ld |
Move | mov | Store | st |
Addition | add | Jump | jmp |
Subtraction | sub | Compare | cmp |
Multiply | mul | Conditional Jump | jnz, jz, jg, jl, jge, … |
Divide | div | Bitwise AND | and |
Increment | inc | Bitwise OR | or |
Decrement | dec | Bitwise XOR | xor |
Call | call | Shift Left | shl |
Return | ret | Shift Right | shr |
No Operation | nop | Input | in |
Halt | hlt | Output | out |
Push | push | Pop | pop |
Basic Assembly Language Program
Now we will add two numbers in CPU registers using assembly language.
[org 0x0100]
mov ax, 5 ; load first number in ax
mov bx, 10 ; load second number in bx
add ax, bx ; accumulate sum in ax
mov bx, 15 ; load third number in bx
add ax, bx ; accumulate sum in ax (ax=ax+bx)
mov ax, 0x4c00
int 0x21
- The first and last two lines are same as explained previously. In the body of the ASM program I have written some instructions.
- mov ax, 5 means that move the number 5 in the register ax. Our value 5 now move to register ax, and it stays as long as we or program don’t change it.
- mov bx, 5 are also doing the same thing as previous, but it is adding 5 in our bx register.
- add ax, bx will add the content of the register ax and bx and store the result in the ax register which is destination that we learn in instruction format.
- mov bx, 15 will move he 15 in bx register and the content of bx register will be overwritten.
- add ax, bx again add the content of register ax and bx, and store result in source ax register.
Compiling Assembly Language Code
After writing your program you need to compile it and then run the program in the AFD Debugger so you can see the result. To do so, follow the instructions below for your every assembly program.
1. Open the DOSBox software and run the following commands once only.
mount x c:/asm
x:
2. Then write the following command, make sure to change the file_name word with your ASM file name.
nasm file_name.asm -o file_name.com -l file_name.lst
3. Now write the below command to open this in the AFD (A/Advanced Full Screen) debugger.
afd file_name.com
Now you can see the debugger screen and you can press F1 to iterate to the statements that we written in program and see the register AX, BX values.
Now, we will continue this article tomorrow. Have a great day of learning!