Title: DEBUG
1DEBUG
2DEBUG
- Assembly programmers can write instructions
symbolically the assembler will encode the
symbolic instructions, outputting the actual
machine instructions (storing them, after
linking, in an EXE file) - The CPU comprises several registers which are
places where data can be stored. Registers are
like variables. Indeed, in the C language you
can even request (using the register keyword)
that the compiler use a register to store a
particular variable (C will try to accommodate
you).
3DEBUG
- The CPU is connected to memory by a bus. The bus
carries data, address information, and control
signals. - Computer programs are stored in memory in RAM
(random access memory). So the CPU must fetch
each instruction, decode it, execute it, and
fetch the next instruction. (It keeps the
address of the next instruction in its IP,
instruction pointer, register). Certain kinds of
instructions called jumps can cause the CPU to
fetch an instruction from anyplace in the
program. -
4REGISTERS
- We shall now concentrate on the CPU, specifically
on registers. - 8086 Processor Registers
- The ubiquitous Intel 8086 was introduced in
1978. An enormous customer base has ensured that
software is still written to the 8086 standard . - Programmers unanimously agree while Intel 8086
family processors are well designed and
versatile, there are just not enough CPU
registers.
5REGISTERS
- This Great register shortage is not enough to
thwart the dedicated Assembly language
programmer, who takes the time to understand and
make the most efficient use of our limited
resources. Each register has something it does
better than every other register -- sometimes
tricks that no other register can do at all. -
6REGISTERS
- Here are the 16-bit registers available on all
Intel 8086-compatible microprocessors
General Purpose 8086 Registers
7 AX REGISTER
- The four general purpose registers, AX, BX, CX
and DX, work as 16-bit registers, or as two 8-bit
registers you could load a value in AL and a
different one in AH, then access them together as
AX.
AX - Accumulator Register
The workhorse, AX is constantly changing
throughout any program. Other registers are
pointers or counters or constants, that seem to
be there just to service AX, as information flows
through this most useful register. The 8086 has
several hard-wired connections between AX or AL
and other registers
8AX REGISTER
Tip When practical, use AX instead of other
registers. Many instructions work faster with the
AX register. A few instructions have shorter
encoding when used with AX. Note When Assembly
language functions return an integer or string
descriptor to Basic, they always place the value
in AX.
9BX REGISTER
- BX - Base Register
- In addition to general storage, you can use BX
for accessing memory it is the only general
purpose register that also works as a pointer.
Unlike SI and DI, BX doesn't work with the rep
prefix or the fancy string instructions. By
default, memory operations using BX assume the DS
register. You can use a segment override. This
example loads AL with a byte from the current
code segment - mov al, BYTE PTR csbx
- Note In simple routines that have limited direct
access of memory, use BX instead of SI or DI.
This is because high-level 16-bit DOS programming
languages never require you to preserve BX. -
10 BX REGISTER
One powerful yet seldom used instruction is xlat.
Place a pointer in BX, and a character in the AL
register, then execute the xlat instruction. Xlat
uses the value in AL as an index into the table,
replacing the contents of AL with that character.
If AL0, it will be replaced with the first item
in the table, AL1 means the second item, and so
forth. This example translates a string using a
table pointed to by the BX register
mov bx, OFFSET Table Translation table _at__at_
lodsb Read a character to AL
xlatb Translate stosb
Write to output string
loop _at_B Until CX 0
11EBX REGISTER
- EBX is reservedMany 32-bit operating systems
reserve the EBX register. Get into the habit of
preserving the BX register on the stack in
subroutines. (32-bit versions of Microsoft Visual
C for Windows reserve EBX.)
12CX REGISTER
- In addition to general storage, CX (and to a
lesser extent CL) is used as an iteration
counter. The rep prefix, loop instruction, and
shift/rotate instructions all take counts passed
in CX or CL. - This example computes the sum of integer values
in an array. At entry, CX is the number of
integers, and DSBX points to the array. - xor ax, ax Zero-out the accumulator
- jcxz NoLoop If CX 0, don't loop
- _at__at_
- add ax, bx
- add bx, 2 Move the pointer forward
- loop _at_B DEC CX / JNZ LoopTop
13CX REGISTER
- The CL half of CX is used for the count for shift
and rotate instructions. Intel 80186 and later
processors (including the NEC V-20) can shift or
rotate more than one bit at a time without using
CL. - The example shifts AX left five times using CL
- mov cl, 5 Shift 5 times
- shl ax, cl
14CX REGISTER
15 DX REGISTER
- While DX is the least versatile register, that
may be its greatest strength we often use DX as
truly a data storage register, without concern
about shuttling data back and forth. DX is also
the high word in division instructions. After
16-bit division, the remainder is placed in DX.
The DX register is also used with AL and the in
and out instructions to specify the port number. - We use DX along with AX to return long integers
to high-level languages. The most significant
word is in DX, the low word is in AX. When
returning far pointers, the segment is in DX,
while AX contains the offset.
16DX REGISTER
Note When a function returns a short integer, it
is a good idea to clear DX to zero, or to
sign-extend the results into DX (the cwd
instruction). This makes it easy to declare the
function as a long integer or integer (or both --
with an ALIAS) without any additional code
changes. Tip You can zero-out DX with the cwd
(Convert Word to Double) instruction as long as
the high-bit (the sign bit) of AX is zero. cwd
sign-extends the sign bit of AX into all bits of
DX if the high bit of AX is zero, all of DX will
become zero, but if AX is negative, DX will
become -1. If you use this trick, be sure to
explain it in a source code comment. An
interesting side effect is that you can return
zero if AX is negative, or -1 if AX is positive
without doing a conditional with these
instructions cwd Sign extend AX into DX
mov ax, dx Move the word back into AX neg
ax Toggle all bits of AX