very short intro to assembly language programming

26
(Very short ) Introduction to Assembly Language Programming! Ashis Kumer Biswas CSE @ UT Arlington October 24, 2012 Ashis Kumer Biswas Assembly Language Programming

Upload: ashisbiswas

Post on 13-Apr-2015

25 views

Category:

Documents


4 download

DESCRIPTION

Specifically this presentation motivates students to head start exploring the Tanenbaum assembler.

TRANSCRIPT

Page 1: Very Short Intro to Assembly Language Programming

(Very short) Introduction to Assembly

Language Programming!

Ashis Kumer Biswas

CSE @ UT Arlington

October 24, 2012

Ashis Kumer Biswas Assembly Language Programming

Page 2: Very Short Intro to Assembly Language Programming

What is Assembly Language?

It’s a low level programming language. Which level?

Ashis Kumer Biswas Assembly Language Programming

Page 3: Very Short Intro to 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 assemblystatement and the machine instruction. (Interpreter isenough)

Our assembly language: usage of high-level constructs,e.g., variable names, macros, procedures, etc. (hard tointerpret ⇒ requires translation).

Assembler : Our assembly language translator

Ashis Kumer Biswas Assembly Language Programming

Page 4: Very Short Intro to 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 languagestatements.

Tedious

Time consuming

More prone to error,

Debugging is even more tedious

Not suitable for lazy programmers

Ashis Kumer Biswas Assembly Language Programming

Page 5: Very Short Intro to 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 theexecution time!

Where to start?

Ashis Kumer Biswas Assembly Language Programming

Page 6: Very Short Intro to Assembly Language Programming

Why will I learn this?

Size of your assembly program is much smaller than thatof high-level.

Speed and space critical devices use assembly:

RFID cardDevice driversBIOS routines

Ashis Kumer Biswas Assembly Language Programming

Page 7: Very Short Intro to 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 folderstructure:

Ashis Kumer Biswas Assembly Language Programming

Page 8: Very Short Intro to Assembly Language Programming

Installation: Step 1

Create a folder named “tracer” in C: or D: drive.

Copy the contents of “windows” (not the windows folderitself) in 8088 tra.zip into the “tracer” folder.

Make sure you have the following folder structure:

Ashis Kumer Biswas Assembly Language Programming

Page 9: Very Short Intro to Assembly Language Programming

Installation: Step 1

Three EXE files 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 thatyou can execute these three from anywhere usingcommand prompt. [ Please check Addendum-1 ]

Please be familiar with how to type in commands in thecommand prompt

Ashis Kumer Biswas Assembly Language Programming

Page 10: Very Short Intro to Assembly Language Programming

Installation: Step 2 (Windows Only)

Add the following line at the end of “config.nt”

device=%SystemRoot%\system32\ansi.sys︸ ︷︷ ︸also make sure the path of ansi.sys

you need to modify security property of the file“config.nt” in order to add the above line and save thechanges. [ Please check Addendum-3 ]

If you can not modify the “config.nt” file, every outputproduced by the t88 will be shown really garbled (as yousaw during my presentation)!!

Then a reboot (or, a logoff should be sufficient)

Ashis Kumer Biswas Assembly Language Programming

Page 11: Very Short Intro to Assembly Language Programming

Installation in a Linux Machine

You would be happy to install and use it on Linux (particularlyUbuntu 12.04 as there are many good things about it).

Create a directory (i.e., folder) named “tracer” in yourhome directory (e.g., /home/ashis/)

Copy and paste all the contents of the “linux” directoryinside /home/ashis/tracer.

Open up a terminal. Change current working directory to/home/ashis/tracer/bin. You already knew therewould be three files in it:as88, s88, t88

You have to attach the “execute” permission on each ofthese before use. You can do it pretty easily by typingthis single command: chmod +x *

Now update the PATH environment variable in order tobe able to call these assembler and tracers from anyworking directory. [ Please check Addendum-2 ]

Ashis Kumer Biswas Assembly Language Programming

Page 12: Very Short Intro to Assembly Language Programming

Creating binaries using as88

Assembler source files 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 files:

project1.88 ⇒ The 8088 binaryproject1.# ⇒ A file which links the file positions in thesource file to the positions in the binary fileproject1.$ ⇒ A copy of the source file which containssecondary sources and satisfies preferred conventions forthe assembly process.

Ashis Kumer Biswas Assembly Language Programming

Page 13: Very Short Intro to Assembly Language Programming

Tracing/Debugging using t88

To trace a file (already assembled using as88) use thecommand “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 ofwindows, enabling you to observe execution (instructionby instruction).

t88 executes exactly on assembler command when theENTER key is hit.

Type “q” followed by ENTER to stop execution (i.e., stoptracing).

Ashis Kumer Biswas Assembly Language Programming

Page 14: Very Short Intro to Assembly Language Programming

Tracing/Debugging using s88

s88 is almost similar to t88, except that it does not showthe tracer window.

To trace (without the trace window) the previousproject1, we enter the command s88 project1

Ashis Kumer Biswas Assembly Language Programming

Page 15: Very Short Intro to Assembly Language Programming

Details about 8088 processor

It has 14 registers, each 16 bits wide

PC/IP contains the memory location of the nextinstruction to be executed

CS Segment (64KB) and the CS register

Data segment (64KB) and DS register

Ashis Kumer Biswas Assembly Language Programming

Page 16: Very Short Intro to 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 of8-bit registers: AH,AL, BH,BL, CH,CL,DH,DL.

Ashis Kumer Biswas Assembly Language Programming

Page 17: Very Short Intro to Assembly Language Programming

Details about 8088 processor

SP : Stack pointer

Stack is a segment of memory that holds informationwhen a procedure is called. It can contain otherinformation.

PUSH CX , puts the 16-bit content of CX on top of thestack. This instruction first decrements SP by 2, thenstores its operand at the address SP is now pointing to.

POP CX removes a 16-bit content from the top of thestack by fetching the value on top of the stack and thenincrements SP by 2.

There are other special purpose registers: BP (related tothe stack indexing), SI, DI

Ashis Kumer Biswas Assembly Language Programming

Page 18: Very Short Intro to 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

Page 19: Very Short Intro to 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

Page 20: Very Short Intro to 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, SIand DI registers.

Ashis Kumer Biswas Assembly Language Programming

Page 21: Very Short Intro to Assembly Language Programming

Yet another example (add.s)

Ashis Kumer Biswas Assembly Language Programming

Page 22: Very Short Intro to Assembly Language Programming

Where do you get more Information?

The software, documentation, this presentation atBlackboard

Appendix C of your Tanenbaum’s book (6th Edition)

Ashis Kumer Biswas Assembly Language Programming

Page 23: Very Short Intro to Assembly Language Programming

Addendum 1: How to fix the PATH environment in

Windows 7

1

2

34

56

Click Start >Right click on Computerto get this menu

Ashis Kumer Biswas Assembly Language Programming

Page 24: Very Short Intro to Assembly Language Programming

Addendum 2: How to fix the PATH environment in

Linux

Suppose I’ve just unzipped the “8088 tra.zip” into thedirectory “/home/ashis/tracer”, and it contains the contentsof the “linux” directory of the ZIP file, that has the “bin” withthe three executables: as88, s88, t88.

1 Open a terminal.

2 Type the commandexport PATH=$PATH:/home/ashis/tracer/bin

3 This is not a permanent solution, i.e., if you logout thiswill be wiped out.

4 However, if you really want a permanent solution, thenappend the above command in any startup scripts (e.g.,/home/ashis/.bashrc)

Ashis Kumer Biswas Assembly Language Programming

Page 25: Very Short Intro to Assembly Language Programming

Addendum 3: How to bypass security on config.nt

file

1 Click START menu.

2 Type in the search box “config.nt” (without the quotes).Once, you find it at the top of the start menu, right clickon the file name, then select “Properties” from thecontext menu.

3 Click on the “Security” tab.

4 Then select your user name and click the Edit button.

5 Click on the tiny checkboxes in the “Allow” column sothat you have full control of the file.

6 Then click OK.

Ashis Kumer Biswas Assembly Language Programming

Page 26: Very Short Intro to Assembly Language Programming

Thank You

Thank You!Questions please

Ashis Kumer Biswas Assembly Language Programming