(Very short ) Introduction to Assembly Language Programming!
Ashis Kumer Biswas
CSE @ UT Arlington
October 24, 2012
Ashis Kumer Biswas
Assembly Language Programming
What is Assembly Language?
Its a low level programming language. Which level?
Ashis Kumer Biswas
Assembly Language Programming
What is Assembly Language?
Is it an interpreted language? or a translated? Answer: It is a translated language. Why? Our Assembly Language vs. Pure Assembly Language!!! Pure: One-to-one correspondence between the assembly statement and the machine instruction. (Interpreter is enough) Our assembly language: usage of high-level constructs, e.g., variable names, macros, procedures, etc. (hard to interpret requires translation). Assembler : Our assembly language translator
Ashis Kumer Biswas
Assembly Language Programming
Why will I NOT learn this?
A high-level statement (e.g., System.out.println(Pi = +Math.PI); ) around hundreds of assembly language statements. Tedious Time consuming More prone to error, Debugging is even more tedious Not suitable for lazy programmers
Ashis Kumer Biswas
Assembly Language Programming
Why will I learn this?
Final code runs faster if you invest time to code. Why? 10% of the program is responsible for 90% of the execution time! Where to start?
Ashis Kumer Biswas
Assembly Language Programming
Why will I learn this?
Size of your assembly program is much smaller than that of high-level. Speed and space critical devices use assembly:
RFID card Device drivers BIOS routines
Ashis Kumer Biswas
Assembly Language Programming
Installation: Step 1
We will be using 8088 Assembler and Tracer Toolkit (Check course materials @Blackboard) Download and extract 8088 tra.zip. The folder structure:
Ashis Kumer Biswas
Assembly Language Programming
Installation: Step 1
Create a folder named tracer in C: or D: drive. Copy the contents of windows (not the windows folder itself) in 8088 tra.zip into the tracer folder. Make sure you have the following folder structure:
Ashis Kumer Biswas
Assembly Language Programming
Installation: Step 1
Three EXE les in C:\tracer\bin folder as88 assembler s88 interpreter t88 debugger-tracer The interpreter s88 is for debugging purpose along with t88. Add C:\tracer\bin folder in PATH environment, so that you can execute these three from anywhere using command prompt. [ Please check Addendum-1 ] Please be familiar with how to type in commands in the command prompt
Ashis Kumer Biswas
Assembly Language Programming
Installation: Step 2 (Windows Only)
Add the following line at the end of cong.nt device=%SystemRoot%\system32\ansi.sys
also make sure the path of ansi.sys
you need to modify security property of the le cong.nt in order to add the above line and save the changes. [ Please check Addendum-3 ] If you can not modify the cong.nt le, every output produced by the t88 will be shown really garbled (as you saw during my presentation)!! Then a reboot (or, a logo should be sucient)
Ashis Kumer Biswas
Assembly Language Programming
Installation in a Linux Machine
You would be happy to install and use it on Linux (particularly Ubuntu 12.04 as there are many good things about it). Create a directory (i.e., folder) named tracer in your home directory (e.g., /home/ashis/) Copy and paste all the contents of the linux directory inside /home/ashis/tracer. Open up a terminal. Change current working directory to /home/ashis/tracer/bin. You already knew there would be three les in it:as88, s88, t88 You have to attach the execute permission on each of these before use. You can do it pretty easily by typing this single command: chmod +x * Now update the PATH environment variable in order to be able to call these assembler and tracers from any working directory. [ Please check Addendum-2 ]
Ashis Kumer Biswas Assembly Language Programming
Creating binaries using as88
Assembler source les have an extension .s To create a binary for a source code named project1.s, enter the command as88 project1 It performs the assembly and generate three les:
project1.88 The 8088 binary project1.# A le which links the le positions in the source le to the positions in the binary le project1.$ A copy of the source le which contains secondary sources and satises preferred conventions for the assembly process.
Ashis Kumer Biswas
Assembly Language Programming
Tracing/Debugging using t88
To trace a le (already assembled using as88) use the command t88. For example, let us trace project1 t88 project1 The command above will display the registers, stack, portions of memory, and other information in a set of windows, enabling you to observe execution (instruction by instruction). t88 executes exactly on assembler command when the ENTER key is hit. Type q followed by ENTER to stop execution (i.e., stop tracing).
Ashis Kumer Biswas
Assembly Language Programming
Tracing/Debugging using s88
s88 is almost similar to t88, except that it does not show the tracer window. To trace (without the trace window) the previous project1, we enter the command s88 project1
Ashis Kumer Biswas
Assembly Language Programming
Details about 8088 processor
It has 14 registers, each 16 bits wide PC/IP contains the memory location of the next instruction to be executed CS Segment (64KB) and the CS register Data segment (64KB) and DS register
Ashis Kumer Biswas
Assembly Language Programming
Details about 8088 processor
AX,BX,CX and DX are the general registers. Each of these general registers can be treated as a pair of 8-bit registers: AH,AL, BH,BL, CH,CL,DH,DL.
Ashis Kumer Biswas
Assembly Language Programming
Details about 8088 processor
SP : Stack pointer Stack is a segment of memory that holds information when a procedure is called. It can contain other information. PUSH CX , puts the 16-bit content of CX on top of the stack. This instruction rst decrements SP by 2, then stores its operand at the address SP is now pointing to. POP CX removes a 16-bit content from the top of the stack by fetching the value on top of the stack and then increments SP by 2. There are other special purpose registers: BP (related to the stack indexing), SI, DI
Ashis Kumer Biswas
Assembly Language Programming
Hello World Example (hello.s)
Symbol definitions
Text Section (Codes)
Data Section (Init Memory)
Block started by Symbol (reserve space in data segment)
Ashis Kumer Biswas Assembly Language Programming
Assembling the Hello World Code (hello.s)
Change cwd to root drive (e.g., C:\)
Change cwd to C:\tracer folder Change cwd to C:\tracer\myexamples folder
Assemble the hello.s source code
*cwd = current working directory
Ashis Kumer Biswas
Assembly Language Programming
Something about Addressing
MOV AX, BX ADD CX, 20 Direct Addressing example ADD CX, (20) Register indirect Addressing example ADD DX, (BX) MOV CX,5(SI) Register indirect Addressing is possible with only BX, SI and DI registers.
Ashis Kumer Biswas
Assembly Language Programming
Yet another example (add.s)
Ashis Kumer Biswas
Assembly Language Programming
Where do you get more Information?
The software, documentation, this presentation at Blackboard Appendix C of your Tanenbaums book (6th Edition)
Ashis Kumer Biswas
Assembly Language Programming
Addendum 1: How to x the PATH environment in Windows 7
Click Start > Right click on Computer to get this menu
4 5 6
Ashis Kumer Biswas Assembly Language Programming
Addendum 2: How to x the PATH environment in Linux
Suppose Ive just unzipped the 8088 tra.zip into the directory /home/ashis/tracer, and it contains the contents of the linux directory of the ZIP le, that has the bin with the three executables: as88, s88, t88. 1 Open a terminal. 2 Type the command export PATH=$PATH:/home/ashis/tracer/bin
3
This is not a permanent solution, i.e., if you logout this will be wiped out. However, if you really want a permanent solution, then append the above command in any startup scripts (e.g., /home/ashis/.bashrc)
Ashis Kumer Biswas Assembly Language Programming
Addendum 3: How to bypass security on cong.nt le
1 2
3 4 5
Click START menu. Type in the search box cong.nt (without the quotes). Once, you nd it at the top of the start menu, right click on the le name, then select Properties from the context menu. Click on the Security tab. Then select your user name and click the Edit button. Click on the tiny checkboxes in the Allow column so that you have full control of the le. Then click OK.
Ashis Kumer Biswas
Assembly Language Programming
Thank You
Thank You! Questions please
Ashis Kumer Biswas
Assembly Language Programming