Ics 232 Lab 1

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

ICS 232 Lab 1

Pentium Assembly Language Programming using DOS


Debugger
The first reason to work with assembler is that it provides the opportunity of knowing
more the operation of your PC, which allows the development of software in a more
consistent manner. The second reason is the total control of the PC which you can have
with the use of the assembler. Another reason is that the assembly programs are quicker,
smaller, and have larger capacities than ones created with other languages. Lastly, the
assembler allows an ideal optimization in programs, be it on their size or on their
execution.

Assembler language Basic concepts


Debug program
To create a program in assembler two options exist, the first one is to use the TASM or
Turbo Assembler, of Borland, and the second one is to use the debugger - on this first
section we will use this last one since it is found in any PC with the MS-DOS, which
makes it available to any user who has access to a machine with these characteristics.

Debug can only create files with a .COM extension, and because of the characteristics of
these kinds of programs they cannot be larger that 64 kb, and they also must start with
displacement, offset, or 0100H memory direction inside the specific segment.

Debug provides a set of commands that lets you perform a number of useful operations:

A Assemble symbolic instructions into machine code


D Display the contents of an area of memory
E Enter data into memory, beginning at a specific location
N Name a program
Q Quit the debug program
R Display the contents of one or more registers
T Trace the contents of one instruction
U Unassembled machine code into symbolic code
W Write a program onto disk
H Show the addition and subtraction of two numbers in hexadecimal. Used to obtain the
length of a program or an instruction.

It is possible to visualize the values of the internal registers of the CPU using the Debug
program. To begin working with Debug, type the following prompt in your computer:

C:/>Debug [Enter]

On the next line a dash will appear, this is the indicator of Debug, at this moment the
instructions of Debug can be introduced using the following command:

1
-r[Enter]

AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000


SI=0000 DI=0000
DS=0D62 ES=0D62 SS=0D62 CS=0D62 IP=0100 NV EI PL NZ
NA PO NC
0D62:0100 2E CS:
0D62:0101 803ED3DF00 CMP BYTE PTR [DFD3],00
CS:DFD3=03

All the contents of the internal registers of the CPU are displayed; an alternative of
viewing them is to use the "r" command using as a parameter the name of the register
whose value wants to be seen. For example:

-rbx
BX 0000
:

This instruction will only display the content of the BX register and the Debug indicator
changes from "-" to ":"

When the prompt is like this, it is possible to change the value of the register which was
seen by typing the new value and [Enter], or the old value can be left by pressing [Enter]
without typing any other value.

CPU Registers

The 8086 CPU is 16 bit processor, which has 4 internal registers, each one of 16 bits. The
first four, AX, BX, CX, and DX are general use registers and can also be used as 8 bit
registers, if used in such a way it is necessary to refer to them for example as: AH and
AL, which are the high and low bytes of the AX register. This nomenclature is also
applicable to the BX, CX, and DX registers.

The registers known by their specific names:

AX Accumulator : favored by CPU for arithmetic operations.

BX Base register :
-Can hold the address of a procedure or variable (SI, DI, and BP can also).
-Can also perform arithmetic and data movement.

CX Counting register: acts as a counter for repeating or looping instructions.

DX Data register : holds the high 16 bits of the product in multiply (also handles
divide operations)

2
DS Data Segment register : default base location for variables

ES Extra Segment register : additional base location for memory variables.

SS Stack Segment register: Base location of the stack.

CS Code Segment register: holds base location for all executable instructions in a
program.

BP Base Pointers register : contains an assumed offset from the SS register. Often
used by a subroutine to locate variables that were passed on the stack by a calling
program.

SI Source Index register : Used in string movement instructions. The source string is
pointed to by the SI register.

DI Destination Index register: acts as the destination for string movement


instructions.

SP Stack pointer register: Contains the offset of the top of the stack.

IP Next Instruction Pointer register: contains the offset of the next instruction to be
executed.

F Flag register: individual bit positions within register show status of CPU or results
of arithmetic operations.

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.

3
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 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 this is done, the screen will
produce the following 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

4
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 moves 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:

1. Obtain the length of the program subtracting the final address


from the initial address, naturally in hexadecimal system.

2. Give the program a name and extension.

3. Put the length of the program on the CX register and 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:

5
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:

1. Give the name of the file to be loaded.


2. 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:

6
-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.

Activity 1.1: Enter the following program instructions in assembly code at the offset
memory location 100h by typing A 100 at the DEBUG program prompt then press
Enter-key.

MOV AX, 2864


ADD AX, 3749
MOV BX, AX
SUB BX, 2805
NOP

Activity 1.2: Use DEBUG command U 100 to unassembled the instructions in Activity
1.1. What is the machine code corresponding to each assembly code instruction?

Assembly code Machine Code


MOV AX, 2864
ADD AX, 3749
MOV BX, AX
SUB BX, 2805
NOP

Activity 1.3: How many bytes does it need to represent each instruction in binary?

Assembly code Number of bytes


MOV AX, 2864
ADD AX, 3749
MOV BX, AX
SUB BX, 2805
NOP
Activity 1.4: What are the contents of CS, IP, AX, and BX? Use DEBUG command R to
display these information?

7
Register Content
CS
IP
AX
BX

Activity 1.5: Predict the contents of the following registers after execution of each
instruction: CS, IP, AX, and BX.

Register MOV AX, 2864 ADD AX, 3749 MOV BX, AX SUB BX, 2805
CS
IP
AX
BX

8
Debug Flag symbols
Status Flag Set (1) Symbol Clear (0) Symbol
CF CY(Carry) NC(No carry)
PF PE (Even parity) PO (odd parity)
AF AC(Auxiliary carry) NA (No Auxiliary carry)
ZF ZR(zero) NZ(No zero)
SF NG(negative) PL (plus)
OF OV(Overflow) NV(no overflow)
Control Flags
DF DN(Down) UP(up)
IF EI(Enable Interrupt) DI(disable interrupt)

Notes:
- Carry (CF) – set when the result of an unsigned arithmetic operation is too
large to fit into the destination.

- Parity(PF) – reflects whether the number of 1 bits in the result of an


operation is even or odd. 1 – odd, 0-even.

- Auxiliary Carry(AF) – set when the result of an operation causes a carry


from bit 3 to bit 4.

- Zero(ZF) – set when the result of an arithmetic or logical operation is zero.

- Sign(SF) – set when the result of an arithmetic or logical operation generates


a negative result.

- Overflow(OF) – set when the result of a signed arithmetic operation is too


wide to fit into the destination.

- Direction(DF) – affects block data transfer instructions such as MOVS,


CMPS. 0 – up, 1 – down.

- Interrupt(IF) – dictates whether or not system interrupts can occur. 1 –


enabled, 0 – disabled.

You might also like