Tekil Mesaj gösterimi
Alt 06-09-2008, 15:07   #2
Constantin
ยŦยк
 
Constantin - ait Kullanıcı Resmi (Avatar)
 

Assembler structure

In assembly language code lines have two parts, the first one is the name of the instruction which is to be executed, and the second one are the parameters of the command. For example:
add ah bh
Here "add" is the command to be executed, in this case an addition, and "ah" as well as "bh" are the parameters.

For example:
mov al, 25
In the above example, we are using the instruction mov, it means move the value 25 to al register.

The name of the instructions in this language is made of two, three or four letters. These instructions are also called mnemonic names or operation codes, since they represent a function the processor will perform.

Sometimes instructions are used as follows:
add al,[170]
The brackets in the second parameter indicate to us that we are going to work with the content of the memory cell number 170 and not with the 170 value, this is known as direct addressing.

Creating basic assembler program

The first step is to initiate the Debug, this step only consists of typing
c:\> debug [Enter]
on the operative system prompt.

To assemble a program on the Debug, the "a" (assemble) command is used;
when this command is used, the address where you want the assembling to begin can be given as a parameter, if the parameter is omitted the assembling will be initiated at the locality specified by CS:IP, usually 0100h, which is the locality where programs with .COM extension must be initiated. And it will be the place we will use since only Debug can create this specific type of programs.

Even though at this moment it is not necessary to give the "a" command a parameter, it is recommendable to do so to avoid problems once the CS:IP registers are used, therefore we type:
a 100 [Enter]
mov ax,0002 [Enter]
mov bx,0004 [Enter]
add ax,bx [Enter]
nop [Enter][Enter]
What does the program do?, move the value 0002 to the ax register, move the value 0004 to the bx register, add the contents of the ax and bx registers, the instruction, no operation, to finish the program.

In the debug program. After to do this, appear on the screen some like the follow lines:
C:\> debug
-a 100
0D62:0100 mov ax,0002
0D62:0103 mov bx,0004
0D62:0106 add ax,bx
0D62:0108 nop
0D62:0109
Type the command "t" (trace), to execute each instruction of this program, example:
-t
AX=0002 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=0D62 ES=0D62 SS=0D62 CS=0D62 IP=0103 NV EI PL NZ NA PO NC
0D62:0103 BB0400 MOV BX,0004
You see that the value 2 move to AX register. Type the command "t" (trace), again, and you see the second instruction is executed.
-t
AX=0002 BX=0004 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=0D62 ES=0D62 SS=0D62 CS=0D62 IP=0106 NV EI PL NZ NA PO NC
0D62:0106 01D8 ADD AX,BX
Type the command "t" (trace) to see the instruction add is executed, you will see the follow lines:
-t
AX=0006 BX=0004 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=0D62 ES=0D62 SS=0D62 CS=0D62 IP=0108 NV EI PL NZ NA PE NC
0D62:0108 90 NOP
The possibility that the registers contain different values exists, but AX and BX must be the same, since they are the ones we just modified.

To exit Debug use the "q" (quit) command.


Storing and loading the programs

It would not seem practical to type an entire program each time it is needed, and to avoid this it is possible to store a program on the disk, with the enormous advantage that by being already assembled it will not be necessary to run Debug again to execute it.

The steps to save a program that it is already stored on memory are:

Obtain the length of the program subtracting the final address from the initial address, naturally in hexadecimal system.
Give the program a name and extension.
Put the length of the program on the CX register.
Order Debug to write the program on the disk.

By using as an example the following program, we will have a clearer idea of how to take these steps:

When the program is finally assembled it would look like this:
0C1B:0100 mov ax,0002
0C1B:0103 mov bx,0004
0C1B:0106 add ax,bx
0C1B:0108 int 20
0C1B:010A
To obtain the length of a program the "h" command is used, since it will show us the addition and subtraction of two numbers in hexadecimal. To obtain the length of ours, we give it as parameters the value of our program's final address (10A), and the program's initial address (100). The
first result the command shows us is the addition of the parameters and the second is the subtraction.
-h 10a 100
020a 000a
The "n" command allows us to name the program.
-n test.com
The "rcx" command allows us to change the content of the CX register to the value we obtained from the size of the file with "h", in this case 000a, since the result of the subtraction of the final address from the initial address.
-rcx
CX 0000
:000a
Lastly, the "w" command writes our program on the disk, indicating how many bytes it wrote.
-w
Writing 000A bytes
To save an already loaded file two steps are necessary:
Give the name of the file to be loaded.
Load it using the "l" (load) command.
To obtain the correct result of the following steps, it is necessary that the above program be already created.
Inside Debug we write the following:
-n test.com
-l
-u 100 109
0C3D:0100 B80200 MOV AX,0002
0C3D:0103 BB0400 MOV BX,0004
0C3D:0106 01D8 ADD AX,BX
0C3D:0108 CD20 INT 20
The last "u" command is used to verify that the program was loaded on memory. What it does is that it disassembles the code and shows it disassembled. The parameters indicate to Debug from where and to where to disassemble.
Debug always loads the programs on memory on the address 100H, otherwise indicated.


Assembler Programming

• Building Assembler programs
o Needed software
o Assembler Programming
• Assembly process
o Segments
o Table of symbols

• More assembler programs

• Types of instructions
o Data movement
o Logic and arithmetic operations
o Jumps, loops and procedures
________________________________________
Building Assembler programs

Needed software

In order to be able to create a program, several tools are needed:

First an editor to create the source program. Second a compiler, which is nothing more than a program that "translates" the source program into an object program. And third, a linker that generates the executable program from the object program.

The editor can be any text editor at hand, and as a compiler we will use the TASM macro assembler from Borland, and as a linker we will use the Tlink program.

The extension used so that TASM recognizes the source programs in assembler is .ASM; once translated the source program, the TASM creates a file with the .OBJ extension, this file contains an "intermediate format" of the program, called like this because it is not executable yet but it is not a program in source language either anymore. The linker generates, from a .OBJ or a combination of several of these files, an executable program, whose extension usually is .EXE though it can also be .COM, depending of the form it was assembled.

Assembler Programming

To build assembler programs using TASM programs is a different program structure than from using debug program.

It's important to include the following assembler directives:
.MODEL SMALL
Assembler directive that defines the memory model to use in the program

.CODE
Assembler directive that defines the program instructions

.STACK
Assembler directive that reserves a memory space for program instructions in
the stack

END
Assembler directive that finishes the assembler program
Let's program

First step
use any editor program to create the source file. Type the following lines:

First example
; use ; to put comments in the assembler program
.MODEL SMALL ;memory model
.STACK ;memory space for program instructions in the stack
.CODE ;the following lines are program instructions
mov ah,1h ;moves the value 1h to register ah
mov cx,07h ;moves the value 07h to register cx
int 10h ;10h interruption
mov ah,4ch ;moves the value 4 ch to register ah
int 21h ;21h interruption
END ;finishes the program code
This assembler program changes the size of the computer cursor.

Second step

Save the file with the following name: examp1.asm Don't forget to save this in ASCII format.

Third step

Use the TASM program to build the object program.

Example:
C:\>tasm exam1.asm
Turbo Assembler Version 2.0 Copyright (c) 1988, 1990 Borland
International

Assembling file: exam1.asm
Error messages: None
Warning messages: None
Passes: 1
Remaining memory: 471k
The TASM can only create programs in .OBJ format, which are not executable by themselves, but rather it is necessary to have a linker which generates the executable code.

Fourth step

Use the TLINK program to build the executable program example:
C:\>tlink exam1.obj
Turbo Link Version 3.0 Copyright (c) 1987, 1990 Borland
International

C:\>
Where exam1.obj is the name of the intermediate program, .OBJ. This generates a file directly with the name of the intermediate program and the .EXE extension.

Fifth step

Execute the executable program
C:\>exam1[enter]
Remember, this assembler program changes the size of the cursor.

Assembly process.

Segments

The architecture of the x86 processors forces to the use of memory segments to manage the information, the size of these segments is of 64kb.

The reason of being of these segments is that, considering that the maximum size of a number that the processor can manage is given by a word of 16 bits or register, it would not be possible to access more than 65536 localities of memory using only one of these registers, but now, if the
PC's memory is divided into groups or segments, each one of 65536 localities, and we use an address on an exclusive register to find each segment, and then we make each address of a specific slot with two registers, it is possible for us to access a quantity of 4294967296 bytes of memory, which is, in the present day, more memory than what we will see installed in a PC.

In order for the assembler to be able to manage the data, it is necessary that each piece of information or instruction be found in the area that corresponds to its respective segments. The assembler accesses this information taking into account the localization of the segment, given by the DS, ES, SS and CS registers and inside the register the address of the specified piece of information. It is because of this that when we create a program using the Debug on each line that we assemble, something like this appears:
1CB0:0102 MOV AX,BX
Where the first number, 1CB0, corresponds to the memory segment being used, the second one refers to the address inside this segment, and the instructions which will be stored from that address follow. The way to indicate to the assembler with which of the segments we will work with is with the .CODE, .DATA and .STACK directives.

The assembler adjusts the size of the segments taking as a base the number of bytes each assembled instruction needs, since it would be a waste of memory to use the whole segments. For example, if a program only needs 10kb to store data, the data segment will only be of 10kb and not the 64kb it can handle.
Constantin Ofline   Alıntı ile Cevapla