assembly level language

22
Assembly Language Programming Prepared by pdfshare

Upload: pdfshare

Post on 20-Jan-2017

202 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Assembly level language

Assembly Language Programming

Prepared by pdfshare

Page 2: Assembly level language

What is Assembly Language?

Each personal computer has a microprocessor that manages the

computer's arithmetical, logical and control activities.

Each family of processors has its own set of instructions for handling

various operations like getting input from keyboard, displaying

information on screen and performing various other jobs.

These set of instructions are called machine language instruction'.

Processor understands only machine language instructions which are

strings of 1s and 0s. However machine language is too obscure and

complex for using in software development. So the low level assembly

language is designed for a specific family of processors that represents

various instructions in symbolic code and a more understandable form.

Prepared by pdfshare

Page 3: Assembly level language

Advantages of Assembly Language

An understanding of assembly language provides knowledge of:

Interface of programs with OS, processor and BIOS;

Representation of data in memory and other external devices;

How processor accesses and executes instruction;

How instructions accesses and process data;

How a program access external devices.

Other advantages of using assembly language are:

It requires less memory and execution time;

It allows hardware-specific complex jobs in an easier way;

It is suitable for time-critical jobs;

Prepared by pdfshare

Page 4: Assembly level language

Assemblers

Assemblers need to

translate assembly instructions and pseudo-instructions into

machine instructions.

Convert decimal numbers, etc. specified by programmer into

binary

Typically, assemblers make two passes over the assembly file

First pass: reads each line and records labels in a symbol table

Second pass: use info in symbol table to produce actual

machine code for each line

Prepared by pdfshare

Page 5: Assembly level language

Linker

• Tool that merges the object files produced by separate compilation or

assembly and creates an executable file.

• Three tasks

Searches the program to find library routines used by program , e.g.

printf(), math routines,…

Determines the memory locations that code from each module will

occupy and relocates its instructions by adjusting absolute references.

Resolves references among files

Prepared by pdfshare

Page 6: Assembly level language

Debuggers

A program needed when writing any type of code

Displays the contents of memory

Lets you view registers and variables and see how they change

Allows tracing (stepping through a program one line at a time) .

A debugger supplied with both DOS and Windows

Debug.exe

Found in \Windows\command

Command line driven

A precursor of Microsoft Codeview, Borland Turbo Debugger, Visual Studio Debuggers, Periscope, Atron, SYMDEB, Codesmith-86, Advanced-Trace-86

Prepared by pdfshare

Page 7: Assembly level language

Assembly Level Debugger

Debug is an assembly level debugger

Displays only assembly mnemonics and machine instructions.

C> debug sample.exe

Debug.exe

Sample.exe

DOS 0000 Prepared by pdfshare

Page 8: Assembly level language

Debugging Functions

Assemble short programs

View a program’s source code along with its machine language

View the CPU registers and flags

Trace or execute a program, watching variables for changes

Enter new values into memory

Search for binary or ASCII values in memory

Move a block of memory from one location to another

Fill a block of memory

Load and write disk files and sectors

Prepared by pdfshare

Page 9: Assembly level language

Procedure

Definition of procedure

A procedure is a collection of instructions to which we can direct the

flow of our program, and once the execution of these instructions is

over control is given back to the next line to process of the code

which called on the procedure.

Procedures help us to create legible and easy to modify programs.

At the time of invoking a procedure the address of the next

instruction of the program is kept on the stack so that, once the flow

of the program has been transferred and the procedure is done, one

can return to the next line

of the original program, the one which called the procedure.

Prepared by pdfshare

Page 10: Assembly level language

Procedure

A procedure begins with the PROC directive and ends with the ENDP directive.

each directive appears with the procedure name

PROC is followed by the type of procedure:

NEAR or FAR

In MASM version 6.x, the NEAR or FAR type can be followed by the USES

statement.

USES allows any number of registers to be automatically pushed to the stack

and popped from the stack within the procedure

Prepared by pdfshare

Page 11: Assembly level language

CALL

Transfers the flow of the program to the procedure.

CALL instruction differs from the jump instruction because a CALL

saves a return address on the stack.

The return address returns control to the instruction that

immediately follows the

CALL in a program when a RET instruction executes.

Prepared by pdfshare

Page 12: Assembly level language

NEAR CALL

3 bytes long.

the first byte contains the opcode; the second and third bytes contain the displacement

When the near CALL executes, it first pushes the offset address of the next instruction onto the stack.

offset address of the next instruction appears in the instruction pointer (IP or EIP)

It then adds displacement from bytes 2 & 3 to the IP to transfer control to the procedure.

Why save the IP or EIP on the stack?

the instruction pointer always points to the next instruction in the program

For the CALL instruction, the contents of IP/EIP are pushed onto the stack.

program control passes to the instruction following the CALL after a procedure ends

Prepared by pdfshare

Page 13: Assembly level language

FAR CALL

5-byte instruction contains an opcode followed by the next value for the IP and CS

registers.

bytes 2 and 3 contain new contents of the IP

bytes 4 and 5 contain the new contents for CS

Far CALL places the contents of both IP and CS on the stack before jumping to the address

indicated by bytes 2 through 5.

This allows far CALL to call a procedure located anywhere in the memory and return from

that procedure.

The program branches to the procedure.

A variant of far call exists as CALLF, but should be avoided in favor of defining the

type of call instruction with the PROC statement

In 64-bit mode a far call is to any memory location and information placed onto the stack is

an 8-byte number.

the far return instruction retrieves an 8-byte return address from the stack and places it

into RIP

Prepared by pdfshare

Page 14: Assembly level language

Memory Initialization/Reservation

These directives will initialize or reserve memory space in the

form of a byte, a word, or a double word in the code space.

The directives for memory initialization and reservation are DB,

DW and DD

These directives will initialize and reserve memory storage in the

form of a byte, a word or a double word in code space

The directive to reserve memory without initialization is DS

This directive will reserve specified number of bytes in the

current segment

Prepared by pdfshare

Page 15: Assembly level language

DB (Define Byte)

The DB directive initializes code memory with a byte value

The directive has the following format:

<label>: DB <expression>, <expression>, …

label

is the starting address where the byte values are stored

expression

is the byte value, it can be a character string, a symbol, or an 8-bit constant

Prepared by pdfshare

Page 16: Assembly level language

DB (Define Byte)

Example:

CSEG AT 200H

MSG: DB ‘Please enter your password’, 0

ARRAY: DB 10H,20H,30H,40H,50H

The above string of characters will be stored as ASCII bytes starting from location

200H, which means location [200H]=50H, [201H]=6CH and so on

Notice that the DB directive can only be declared in a code segment

If it is defined in a different segment, the assembler will generate an error Prepared by pdfshare

Page 17: Assembly level language

DW (Define Word)

The DW directive initializes the code memory with a double byte or a 16-bit word

The directive has the following format:

<label>: DW <expression>, <expression>, …

Example:

;2 words allocated

CNTVAL: DW 1025H, 2340H

;10 values of 1234H starting from location XLOC

XLOC: DW 10 DUP (1234H)

The DUP operator can be used to duplicate a sequence of memory contents

The DW directive can only be used in the code segment

If it is defined in other segments, the assembler will give an error message

Prepared by pdfshare

Page 18: Assembly level language

DD (Define Double Word)

The DD directive initializes the code memory with double word or 32-bit data

value

The directive has the following format:

<label>: DD <expression>, <expression>, …

Example:

ADDR: DD 820056EFH, 10203040H

EMPT: DD 3 DUP ( 0 )

Same as the DB and DW directives, DD can only be specified in the code segment

If it is declared in other segment it risks having error message generated by

the assembler Prepared by pdfshare

Page 19: Assembly level language

DS (Define Storage)

The DS directive reserves a specified number of bytes in the current segment

It can only be used in the currently active segment like CSEG, ISEG, DSEG or

XSEG

The DS directive has the following format:

<label>: DS <expression>

The expression can not contain forward references, relocatable symbols or

external symbols

Prepared by pdfshare

Page 20: Assembly level language

DS (Define Storage)

Example:

XSEG AT 1000H ;select memory block from ;external memory, starting ;address from 1000H

Input: DS 16 ; reserve 16 bytes

Wavetyp: DS 1 ; reserve 1 byte

The location counter of the segment is incremented by one byte every time the DS statement is encountered in the program

The programmer should be aware that no more than 16 byte values should be entered starting from the address ‘Input’ as shown in the above example

Notice that the bytes are not initialized, just reserved Prepared by pdfshare

Page 21: Assembly level language

Macro

Definition of the macro

A macro is a group of repetitive instructions in a program which are

codified only once and can be used as many times as necessary.

The main difference between a macro and a procedure is that in the macro

the passage of parameters is possible and in the procedure it is not, this

is only applicable for the TASM - there are other programming languages

which do allow it. At the moment the macro is executed each parameter is

substituted by the name or value specified at the time of the call.

We can say then that a procedure is an extension of a determined program,

while the macro is a module with specific functions which can be used by

different programs.

Another difference between a macro and a procedure is the way of calling

each one, to call a procedure the use of a directive is required, on the

other hand the call of macros is done as if it were an assembler

instruction.

Prepared by pdfshare

Page 22: Assembly level language

Prepared By PDFSHARE

Prepared by pdfshare