Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Assignment No.: 01: Name: Shraddha Umesh Mulay Roll No.: 221083 GR No.: 22020260 Sy-A

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Name: Shraddha Umesh Mulay

Roll No.: 221083


GR No.: 22020260
SY-A

Assignment No.: 01
Title: Basics of Assembly Language

Objectives:
1) To study assembly language
2) To study different system calls
3) To study the instruction set

Theory:

What Is Assembly Language:

Assembly language is a low-level programming language which is a correspondence


between machine code and program statement. It is still widely used in academic
applications. The main application of assembly language is – it is used to program devices
and micro-controllers. This is a collection of languages that would be used to write machine
code to build CPU architecture. However, this language would not have enough functions
and variables and cannot be used in all types of processors. The commands and structure of
assembly language are alike to that of machine language, but this lets programmers use
numbers over names.

1) Assembler: The Assembler is used to translate the program written in Assembly


language into machine code. The source program is an input of assembler that
contains assembly language instructions. The output generated by assembler is the
object code or machine code understandable by the computer.

2) Linker: Linker is a program in a system which helps to link a object modules of program into
a single object file. It performs the process of linking. Linker are also called link editors.
Linking is process of collecting and maintaining piece of code and data into a single file.
Linker also link a particular module into system library. It takes object modules from
assembler as input and forms an executable file as output for loader.
Linking is performed at both compile time, when the source code is translated into
machine code and load time, when the program is loaded into memory by the loader.
Linking is performed at the last step in compiling a program.
Linking is of two types:
1. Static Linking -
It is performed during the compilation of source program. Linking is performed
before execution in static linking. It takes collection of relocatable object file and
command-line argument and generate fully linked object file that can be loaded and
run.
Static linker perform two major task:
 Symbol resolution – It associates each symbol reference with exactly one
symbol definition .Every symbol have predefined task.
 Relocation – It relocate code and data section and modify symbol references
to the relocated memory location.

The linker copy all library routines used in the program into executable image. As a
result, it require more memory space. As it does not require the presence of
2. Dynamic linking-
Dynamic linking is performed during the run time. This linking is accomplished by
placing the name of a shareable library in the executable image. There is more
chances of error and failure chances. It require less memory space as multiple
program can share a single copy of the library.

3) Debugger (Turbo debugger): The debugger automatically displays the contents of


memory locations and registers as they are accessed and displays the address of the
program counter. This display makes assembly debugging a valuable tool that you can
use together with source debugging.

4) Assembler Directives and Reserve Directives:


Assembler directives: supply data to the program and control the assembly process.
Assembler directives enable you to do the following:
 Assemble code and data into specified sections
 Reserve space in memory for uninitialized variables
 Control the appearance of listings
 Initialize memory
 Assemble conditional blocks
 Define global variables
 Specify libraries from which the assembler can obtain macros
 Examine symbolic debugging information

Reserve directive: Used to reserve a block of memory.


Ex: NUM1 RESERVE 400 ;declares a memory-block of 400 bytes is to be reserved
for data.
• END directive tells the assembler that this is the end of the source-program text.
• RETURN directive identifies the point at which execution of the program should be
terminated.
5) Fundamental data types.In Assembly language, there are no distinct data types
like char, string, int, float, double, etc. Instead there are very basic data types
according to their sizes. They are:
1. byte – 8 bits
2. word – 16 bits
3. dword – 32 bits
4. qword – 64 bits
5. Real4 – 32 bit float
6. Real8 – 64 bit float
7. Real10 – 80 bit float

6) Macro’s and Procedures:

Procedure: A procedure is group of instructions that usually performs one task. It is


a reusable section of a software program which is stored in memory once but can be
used as often as necessary.

A procedure can be of two types:


1) Near Procedure
2) Far Procedure

Near Procedure: A procedure is known as NEAR procedure if is written(defined) in


the same code segment which is calling that procedure. Only Instruction Pointer(IP
register) contents will be changed in NEAR procedure.

FAR procedure : A procedure is known as FAR procedure if it is written (defined) in


the different code segment than the calling segment. In this case both Instruction
Pointer(IP) and the Code Segment(CS) register content will be changed.

Directives used for procedure:


PROC directive: The PROC directive is used to identify the start of a procedure. The
PROC directive follows a name given to the procedure.After that the term FAR and
NEAR is used to specify the type of the procedure.

ENDP Directive: This directive is used along with the name of the procedure to
indicate the end of a procedure to the assembler. The PROC and ENDP directive are
used to bracket a procedure.

Advantages and Disadvantages of using procedure:

Advantages:
1) Allows to save memory space.
2) Program development becomes easier.
3) Debugging of errors in program become easy.

Disadvantages:
1) CALL and RET instructions are always required to integrate with procedures.
2) Requires the extra time to link procedure and return from it.
3) For small group of instructions, linking and returning back time more than the
execution time, hence for small group of instructions procedures cannot be preffered.
Macro : A MACRO is group of small instructions that usually performs one task. It is
a reusable section of a software program.A macro can be defined anywhere in a
program using directive MACRO &ENDM.

General Form : MACRO-name MACRO [ARGUMENT 1, ............ ARGUMENT N]

MACRO CODING GOES HERE


ENDM

Advantages and disadvantages of MACRO :


Advantages:
1) Program written with macro is more readable.
2) Macro can be called just writing by its name along with parameters, hence no extra
code is required like CALL & RET.
3) Execution time is less because of no linking and returning
4) Finding errors during debugging is easier.

Disadvantages:
1) object code is generated every time a macro is called hence object file becomes
lengthy.
2) For large group of instructions macro cannot be preferred.

7) Basic Syntax of Assembly Language:Assembly language statements are entered one


statement per line. Each statement follows the following format –

[label] mnemonic [operands] [;comment]

8) Compiling and Linking in Assembly Program:Make sure you have set the path
of nasm and ld binaries in your PATH environment variable.
Now, take the following steps for compiling and linking the above program −
 Type the above code using a text editor and save it as hello.asm.
 Make sure that you are in the same directory as where you saved hello.asm.
 To assemble the program, type nasm -f elf hello.asm
 If there is any error, you will be prompted about that at this stage. Otherwise, an object
file of your program named hello.o will be created.
 To link the object file and create an executable file named hello, type ld -m elf_i386 -
s -o hello hello.o
 Execute the program by typing ./hello
If you have done everything correctly, it will display 'Hello, world!' on the screen.
9) Linux Assembly System Calls: 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 −

 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.
There are six registers that store the arguments of the system call used. These are the
EBX, ECX, EDX, ESI, EDI, and EBP. These registers take the consecutive arguments,
starting with the EBX register. If there are more than six arguments, then the memory
location of the first argument is stored in the EBX register.
All the syscalls are listed in /usr/include/asm/unistd.h, together with their numbers (the
value to put in EAX before you call int 80h).

10) Instruction Set


Data Transfer: These instructions are used to transfer the data from the source operand to
the destination operand. Following are the list of instructions under this group −

Instruction to transfer a word

 MOV − Used to copy the byte or word from the provided source to the provided
destination.
 PPUSH − Used to put a word at the top of the stack.
 POP − Used to get a word from the top of the stack to the provided location.
 PUSHA − Used to put all the registers into the stack.
 POPA − Used to get words from the stack to all registers.
 XCHG − Used to exchange the data from two locations.
 XLAT − Used to translate a byte in AL using a table in the memory.

Instructions for input and output port transfer

 IN − Used to read a byte or word from the provided port to the accumulator.
 OUT − Used to send out a byte or word from the accumulator to the provided port.

Instructions to transfer the address

 LEA − Used to load the address of operand into the provided register.
 LDS − Used to load DS register and other provided register from the memory
 LES − Used to load ES register and other provided register from the memory.

Instructions to transfer flag registers


 LAHF − Used to load AH with the low byte of the flag register.
 SAHF − Used to store AH register to low byte of the flag register.
 PUSHF − Used to copy the flag register at the top of the stack.
 POPF − Used to copy a word at the top of the stack to the flag register.

Arithmetic: These instructions are used to perform arithmetic operations like addition,
subtraction, multiplication, division, etc.
Following is the list of instructions under this group −

Instructions to perform addition

 ADD − Used to add the provided byte to byte/word to word.


 ADC − Used to add with carry.
 INC − Used to increment the provided byte/word by 1.
 AAA − Used to adjust ASCII after addition.
 DAA − Used to adjust the decimal after the addition/subtraction operation.

Instructions to perform subtraction

 SUB − Used to subtract the byte from byte/word from word.


 SBB − Used to perform subtraction with borrow.
 DEC − Used to decrement the provided byte/word by 1.
 NPG − Used to negate each bit of the provided byte/word and add 1/2’s complement.
 CMP − Used to compare 2 provided byte/word.
 AAS − Used to adjust ASCII codes after subtraction.
 DAS − Used to adjust decimal after subtraction.

Instruction to perform multiplication

 MUL − Used to multiply unsigned byte by byte/word by word.


 IMUL − Used to multiply signed byte by byte/word by word.
 AAM − Used to adjust ASCII codes after multiplication.

Instructions to perform division

 DIV − Used to divide the unsigned word by byte or unsigned double word by word.
 IDIV − Used to divide the signed word by byte or signed double word by word.
 AAD − Used to adjust ASCII codes after division.
 CBW − Used to fill the upper byte of the word with the copies of sign bit of the lower
byte.
 CWD − Used to fill the upper word of the double word with the sign bit of the lower
word.

Bit Manipulation:
These instructions are used to perform operations where data bits are involved, i.e.
operations like logical, shift, etc.
Following is the list of instructions under this group −

Instructions to perform logical operation

 NOT − Used to invert each bit of a byte or word.


 AND − Used for adding each bit in a byte/word with the corresponding bit in another
byte/word.
 OR − Used to multiply each bit in a byte/word with the corresponding bit in another
byte/word.
 XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with the
corresponding bit in another byte/word.
 TEST − Used to add operands to update flags, without affecting operands.

Instructions to perform shift operations

 SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in LSBs.
 SHR − Used to shift bits of a byte/word towards the right and put zero(S) in MSBs.
 SAR − Used to shift bits of a byte/word towards the right and copy the old MSB into
the new MSB.

Instructions to perform rotate operations

 ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to Carry
Flag [CF].
 ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to
Carry Flag [CF].
 RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to
MSB.
 RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to
LSB.
Program Execution Transfer Instructions: These instructions are used to transfer/branch
the instructions during an execution. It includes the following instructions −
Instructions to transfer the instruction during an execution without any condition −
 CALL − Used to call a procedure and save their return address to the stack.
 RET − Used to return from the procedure to the main program.
 JMP − Used to jump to the provided address to proceed to the next instruction.

Instructions to transfer the instruction during an execution with some conditions −


 JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
 JAE/JNB − Used to jump if above/not below instruction satisfies.
 JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
 JC − Used to jump if carry flag CF = 1
 JE/JZ − Used to jump if equal/zero flag ZF = 1
 JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
 JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.
 JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.
 JLE/JNG − Used to jump if less than/equal/if not greater than instruction satisfies.
 JNC − Used to jump if no carry flag (CF = 0)
 JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
 JNO − Used to jump if no overflow flag OF = 0
 JNP/JPO − Used to jump if not parity/parity odd PF = 0
 JNS − Used to jump if not sign SF = 0
 JO − Used to jump if overflow flag OF = 1
 JP/JPE − Used to jump if parity/parity even PF = 1
 JS − Used to jump if sign flag SF = 1

You might also like