Download MIT Assignment No 1 Writeup

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Title: Introduction to Assembly Language Programming
Aim: To study assembler, linker, masm, tasm and assembly language programming x86
instructions set
Objective:
To be familiar with the format of assembly language program structure and instructions.
Theory:
Assembly Language:
An assembly language is a low-level programming language for computers, microprocessors,
microcontrollers, and other integrated circuits. It implements a symbolic representation of the
binary machine codes and other constants needed to program a given CPU architecture. This
representation is usually defined by the hardware manufacturer, and is based on mnemonics that
symbolize processing steps (instructions), processor registers, memory locations, and other
language features. An assembly language is thus specific to certain physical (or virtual) computer
architecture.
Assembler:
It is a system program which converts the assembly language program instructions into machine
executable instructions. For example: Microsoft Macro Assembler (MASM), Borland Turbo
Assembler (TASM), Open Source Netwide Assembler (NASM) etc.
MASM
The Microsoft Macro Assembler (MASM) is an x86 assembler for MS-DOS and Microsoft
Windows. It supports a wide variety of macro facilities and structured programming idioms,
including high-level functions for looping and procedures. Later versions added the capability of
producing programs for Windows.
TASM
Turbo Assembler (TASM) is an x86 assembler package developed by Borland. It is used with
Borland's high-level language compilers, such as Turbo Pascal, Turbo Basic and Turbo C. The
Turbo Assembler package is bundled with the linker, Turbo Linker, and is interoperable with the
Turbo Debugger.
NASM
The Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture.
It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. NASM is considered
to be one of the most popular assemblers for Linux and is the second most popular assembler
overall, behind MASM.
NASM was originally written by Simon Tatham with assistance from Julian Hall, and is
currently maintained by a small team led by H. Peter Anvin.
Linker
Linker or link editor is a program that takes one or more objects generated by a compiler and
combines them into a single executable program. When a program comprises multiple object
files, the linker combines these files into a unified executable program. Linkers can take objects
from a collection called a library. Some linkers do not include the whole library in the output;
they only include its symbols that are referenced from other object files or libraries.
Loader
A loader is the part of an operating system that is responsible for loading programs, one of the
essential stages in the process of starting a program, it means loader is a program that places
programs into memory and prepares them for execution. Loading a program involves reading the
contents of executable file, the file containing the program text, into memory, and then carrying
out other required preparatory tasks to prepare the executable for running. Once loading is
complete, the operating system starts the program by passing control to the loaded program code.
DOS Debugger “debug” is a command in DOS, MS-DOS, OS/2 and Microsoft Windows (only
x86 versions) which runs the program debug.exe (or DEBUG.COM in older versions of DOS).
Debug can act as an assembler, disassembler, or hex dump program allowing users to
interactively examine memory contents in assembly language, make changes, and selectively
execute COM, EXE and other file types.
Installation of NASM on Linux
1. Download current version of nasm from http://www.nasm.us
Click DOWNLOAD
Open 2.10.07 directory from Index of /pub/nasm/releasebuilds
Click nasm-2.10.07.tar.gz to download nasm for Linux
2. Once you've obtained the Unix/Linux source archive for NASM, nasmXXX.tar.gz (where XXX denotes the version number of NASM contained in the
archive), unpack it into a directory such as /usr/local/src. The archive, when unpacked,
will create its own subdirectory nasm-XXX.
Example nasm-2.10.07
3. NASM is an auto-configuring package: once you've unpacked it, cd to the directory it's
been unpacked into and type ./configure. This shell script will find the best C compiler to
use for building NASM and set up Makefiles accordingly.
4. Once
NASM
has
auto-configured,
you
can
type make to
build
the nasm and ndisasm binaries,
and
then make
install to
install
them
in /usr/local/bin and install the man pages nasm.1 and ndisasm.1 in/usr/local/man/man1.
5. Alternatively, you can give options such as --prefix to the configure script (see the
file INSTALL for more details), or install the programs yourself.
6. NASM also comes with a set of utilities for handling the RDOFF custom object-file
format, which are in the rdoff subdirectory of the NASM archive. You can build these
with make rdf and install them withmake rdf_install, if you want them.
Installations of nasm using rpm file on Linux
 Download recent 64 bit nasm rpm file from www.nasm.us and run it.
 It will automatically install and configure nasm in home directory
Procedure to create and execute a simple assembly program on Ubuntu (Linux) using nasm
Steps to follow 1.
2.
3.
4.
5.
Boot the machine with ubuntu/Fedora
Select and click on <dash home> icon from the toolbar.
Start typing “terminal” . Different terminal windows available will be displayed.
Click on “terminal” icon. A terminal window will open showing command prompt.
Give the following command at the prompt to invoke the editor
gedit hello.asm
6. Type in the program in gedit window, save and exit
7. To assemble the program write the command at the prompt as follows and press enter key
nasm –f elf32 hello.asm –o hello.o (for 32 bit)
nasm –f elf64 hello.asm –o hello.o (for 64 bit)
8. If the execution is error free, it implies hello.o object file has been created.
9. To link and create the executable give the command as
ld –o hello hello.o
gcc –o hell hello.o (if you are using c functions)
10. To execute the program write at the prompt
./hello
11. “hello world” will be displayed at the prompt
The assembly program structure The assembly program can be divided into three sections:
The .data section This section is for "declaring initialized data", in other words defining
"variables" that already contain stuff. However this data does not change at runtime so they're
not really variables. The .data section is used for things like filenames and buffer sizes, and you
can also define constants using the EQU instruction. Here you can use the DB, DW, DD, DQ and
DT instructions. For example:
section .data
message db 'Hello world!'
; Declare message to contain the bytes ;'Hello world!'
msglength equ $-msg
; Declare msglength to have the
buffersize dw 1024
;Declare buffersize to be a word with 1024 bytes
The .bss section This section is where you declare your variables. You use the RESB, RESW,
RESD, RESQ and REST instructions to reserve uninitialized space in memory for your
variables, like this:
section .bss
filename resb 255
; Reserve 255 bytes
number resb 1
; Reserve 1 byte
bignum resw 1
; Reserve 1 word (1 word = 2 bytes)
realarray resq 10
; Reserve an array of 10 reals
The .text section This is where the actual assembly code is written. The .text section must begin
with the declaration global _start, which just tells the kernel where the program execution begins.
(It's like the main function in C or Java, only it's not a function, just a starting point.) Eg.:
section .text
global _start
_start:
. ; Here is the where the program actually begins
Linux System Calls (for 32 bit): (Write system calls that you are using)
You can make use of Linux system calls in your assembly programs. You need to take the
following steps for using Linux system calls in your program:
1.
2.
3.
4.
Put the system call number in the EAX register.
Store the arguments to the system call in the registers EBX, ECX, etc.
Call the relevant interrupt (80h)
The result is usually returned in the EAX register
The following table shows some of the system calls frequently used
The Linux System Calls – (SYSCALL for 64bit execution)
Linux system calls are called in exactly the same way as DOS system calls:
1. write the system call number in RAX
2. set up the arguments to the system call in RDI, RSI, RDX, etc.
3. make a system call with “SYSCALL” instruction
4. The result is usually returned in RAX
Program Development Process:
X86 Instruction Set: Printout of instruction set handouts.
Date:
Assignment No.1
Aim: Write X86/64 Assembly language program (ALP) to add array of N hexadecimal numbers
stored in the memory. Accept input from the user.
Software/Hardware Required:
Core 2 duo/i3/i5/i7 - 64bit processor
Operating System – ubuntu/Fedora 64bit OS
Assembler: NASM
Editor Used – gedit
Theory:
Explanation:
Accept ‘N’, 2digit hex numbers from user which is in Ascii form. Covert it into hex form and
perform addition. Display Carry and Sum on Screen.
Example:
N=3
FF+FF+FF=02FD where Carry=02 and Sum=FD
Assembler Directives Used: (Explain it by your own)
global:
macro:
DB:
.data
.code
.bss
resb
equ
Instructions: (Explain instruction used by your own)
External Functions: No external function is used.
Input: An array entered by the user.
Output: Addition of numbers entered in the array.
Mathematical Model (Draw it on plain paper)
Let y=F(x) be a solution for the above problem.
Let S be a system such that
s={ x1,x2,x3,x4,x5, y , N}
where x1,x2,x3 ,x4,x5 are the inputs to the system
y is the output of system
System performs the addition of set of inputs provided by user and the sum of inputs will be
provided as a output in the name of variable Y.
Y=F(x) where f function performs addition of x inputs and stores the result in variable Y.
X:Set of inputs X:X->so i.e. one to one and onto mapping.
Y:Set of outputs Y:Y->i.e.one to one and onto mapping again.
X-->Y is a many to one mapping and result is a single element.
F: The function F performs addition operation on set of input elements of X.
Deterministic Data analysis
1. If ∃ x(i) such that |X|= 0 return error
2. If ∃x(i) such that |X|≠ 0 then Y=f(X) i.e. addition performed.
Success State
When function addition performs successful addition operation i.e. {Y| X->Y}
End State
When Addition function does not perform addition operation.
x1
x2
F(x)
x3
x4
x5
a)Block Diagram
Y=F(x)
State Diagram
In a state diagram
1.
2.
3.
4.
q0 state indicates the initial state.
q1 state indicates the successful addition operation.
q3 state indicates the final result.
q4 state indicates the termination of operation.
q0
q1
q3
q4
q2
b)State Diagram
Flowchart (Draw it by your own)
Main Algorithm:
1. Start
2. Display Message Enter value of N using display macro
3. Accept Value of N from user (Use Accept macro) and call procedure1
Ascii_to_Hex
4. Move result of step to into counter
5. Intialize Sum=0 and Carry=0
6. Repeat step 5-10until count becomes 0
7. Display message Enter Number using macro1
8. Read number using Macro2 and call procedure 1
9. Add result of Step 7 to Sum using ADD instruction
10. Add Carry Flag value to Carry using ADC instruction
11. Decrement the counter.
12. Display Message “Result” using display macro
13. Move Sum in bl and Carry in bh
14. Call procedure2 Hex_to_Ascii to display result.
15. Stop.
Macros:
Macro 1
1. Name : Display
2. Purpose: to display the messages by replacing the whole code by simple macro
declaration
3. I/P: sys_write call Number i.e eax=4, File descriptor (for Standard output ebx=1), Buffer
Address in ecx, and length of Buffer in edx.
Macro 2
1. Name : Accept
2. Purpose: to accept input from the user by replacing the whole code by simple macro
declaration
3. I/P: sys_write call Number i.e eax=3, File descriptor (for Standard output ebx=0), Buffer
Address in ecx, and length of Buffer in edx.
Procedure: 1
1. Name : Ascii_to_Hex
2. Purpose : Convert 2 Ascii character into 2 digit hex number
3. I/P : Number accepted from user
4. Algorithm for Procedures
a. ESI point to the ascii_num
b. Initialize ecx by 2
c. Initialize bl with 0
d. Rotate left bl by 4 bits
e. Mov value pointed by ESI in al
f. Compare al with 39H
i. If al is below or equal to 39H then sub 30H in al
ii. Else sub 37H in al
g. Add bl with al
h. Move value of al in bl
i. Repeat step d-h until ecx becomes 0
Procedure: 2
1. Name : Hex_to_Ascii
2. Purpose : Convert 4 digit hex number into 4 Ascii character to display Carry and Sum on
Standard output
3. I/P : bl=Sum and bh=Carry
4. Algorithm for Procedures
a. ESI point to the temp
b. Initialize ecx by 4
c. Rotate left bx by 4 bits
d. Mov bl in al
e. Compare al with 39H
i. If al is below or equal to 39H then add 30H in al
ii. Else add 37H in al
f. Move value of al in temp pointed by ESI
g. Increment ESI
h. Repeat step c-h until ecx becomes 0
j. Call Display macro to print result.
Conclusions:
Assembly Level Program to add N Hexadecimal Numbers accepted from user is assembled and
executed successfully.
Frequently Asked Question (Answer following Question)
Q1 What do you mean by initialization of data segment?
Q2 Name Fast addition algorithms
Q3 What is the difference between “ADD” & “ADC” instruction?
Q4 What does u mean by directives?
Q5 Why we indicate FF as 0FF in program?
Q6 What is the difference between “Jump” & “Call” instruction?
Q7 Write down difference between Macro and Procedure.
Q8 What is System Call?
Q9 What is Interrupt?
Q10
What is maximum size of the instruction in 8086 and 80386
Printouts with comments and output