Title: EEL 3801
1EEL 3801
2Data Representation
- Digital computers are binary in nature. They
operate only on 0s and 1s. - Everything must be expressed in terms of 0s
1s - Instructions, data, memory locs. - 1 on ? voltage at output of electronic device
is high (saturated). - 0 off ? voltage at output of electronic
device is zero.
3Data Representation
- The smallest element of information is a bit
0 or 1. But by itself a bit does not convey much
information. Therefore - 8 bits in succession make a byte, the smallest
addressable binary piece of information - 2 bytes (16 bits in succession) make a word
4Data Representation
- 2 words (32 bits in succession) make a double
word. - We can easily understand base 10 numbers. So we
need to learn how to convert between binary and
decimal numbers. - Decimal numbers are not useful to the computer a
compromise base 16 numbers (hexadecimal) and
base 8 nos., or octal.
5Binary Numbers
- Each position in a binary number, starting from
the right and going left, stands for the power of
the number 2 (the base). - The rightmost position represents 20, which 1.
- The second position from the right represents 21
2. - The third position from the right represents 22
4. - The fourth position from the right represents 23
8.
6Binary Numbers
- The value of an individual binary bit is
multiplied by the corresponding base and power of
2, and all the resulting values for all the
digits are added together. For ex. - 0 0 1 0 1 0 0 1
- 027 026 125 024 123 022 021
120 - 0 0 32 0 8 0 0 1 41
7Binary Numbers
- Conversion from decimal to binary - Two methods
- Division by power of 2
- Find the value of the largest power of 2 that
fits into decimal number. - Set 1 for position bit corresponding to that
power. - Subtract that value from the decimal number.
- Go to the first step, and re-do procedure until
remainder is 0
8Binary Numbers
- Example
- decimal number 146
- Largest value of power of 2 that fits into 146 is
128, or 27 - Set 8th bit (power of 7) to 1
- Subtract 128 from 146 18
- Largest value that fits into 18 is 16 or 24
- Set 5th bit (power of 4) to 1
- 18 - 16 2
9Binary Numbers
- Example (continued)
- Largest power of 2 that fits into 2 is 2, or 21
- Set second bit (power of 1) to 1
- Remainder is now 0
- Set all other bits to zero
- The binary equivalent 1 0 0 1 0 0 1 0
10Binary Numbers
- Second Method
- Division by 2
- Integer divide the decimal number by 2.
- Note the remainder (if even, 0 if odd, 1)
- The remainder represents the bit
- Integer divide the quotient again by 2 and note
remainder. - Continue until quotient 0
11Binary Numbers
- Example
- Decimal number 146.
- 146/2 73, remainder 0
- 73/2 36, remainder 1
- 36/2 18, remainder 0
- 18/2 9, remainder 0
- 9/2 4, remainder 1
- 4/2 2, remainder 0
- 2/2 1, remainder 0
12Binary Numbers
- 1/2 0, remainder 1
- 0/2 0, remainder 0
- Starting from the last one, the binary number is
now the string of remainders - 0 1 0 0 1 0 0 1 0
- Since the 0 to the left does not count, we can
lop it off - 1 0 0 1 0 0 1 0
13Hexadecimal Numbers
- Large binary numbers are cumbersome to read.
- gt hexadecimal numbers are used to represent
computer memory and instructions. - Hexadecimal numbers range from 0 to 15 (total of
sixteen). - Octal numbers range from 0 to 7 (total of 8)
14Hexadecimal Numbers
- The letters of the alphabet are used to the
numbers represent 10 through 15. - where A10, B11, C12, D13, E14, and F15
- But why use hexadecimal numbers?
- 4 binary digits (half a byte) can have a maximum
value of 15 (1111), and a minimum value of 0
(0000).
15Hexadecimal Numbers
- If we can break up a byte into halves, the upper
and lower halves, each half would have 4 bits. - A single hexadecimal digit between 0 and F could
more concisely represent the binary number
represented by those half-bytes. - A byte could then be represented by two
hexadecimal digits, rather than 8 bits
16Hexadecimal Numbers
- The advantage becomes more evident for larger
binary numbers - 00010110 00000111 10010100 11101010
- 1 6 0 7 9 4
D A - ? 160794DAh
17Numbers
- A radix is placed after the number to indicate
the base of the number. - These are always in lower case.
- If binary, the radix is a b
- if hexadecimal, h,
- if octal, o or q.
- Decimal is the default, so if it has no
indication, it is assumed to be decimal. A d
can also be used.
18Hexadecimal to Decimal Conversion
- Similarly to binary-to-decimal conversion, each
digit (position from right to left) of the hex
number represents a power of the base (16),
starting with power of 0. - 2 F 5 B ? 2163 15162 5161 11160
24096 15256 516 111 - 8192 3840 80 11 12,123
19Binary Conversion into Hexadecimal
- Binary to hex is somewhat different, because we
in reality, take each 4 bits starting from the
right, and convert it to a decimal number. - We then take the hexadecimal equivalent of the
decimal number (i.e., 10 A, 11 B, etc.) and
assign it to each 4 bit sequence. - Each digit in a hex number hexadized decimal
equivalent of 4 binary bits.
20Hexadecimal Conversion into Binary
- This conversion is also rather simple.
- Each hex digit represents 4 bits. The
corresponding 4-bit binary sequence replaces the
hex digit. For example - 26AF ? 0010 0110 1010 1111
21Signed and Unsigned Integers
- Integers are typically represented by one byte (8
bits) or by one word (16 bits). - There exist two types of binary integers signed
and unsigned
22Unsigned Integers
- Unsigned integers are easy they use all 8 or 16
bits in the byte or word to represent the number. - If a byte, the total range is 0 to 255 (00000000
to 11111111). - If a word, the total range is 0 to 65,535
(0000000000000000 to 1111111111111111).
23Signed Integers
- Are slightly more complicated, as they can only
use 7 or 15 of the bits to represent the number.
The highest bit is used to indicate the sign. - A high bit of 0 ? positive number
- A high bit of 1 ? negative number - counter
intuitive, but more efficient.
24Signed numbers (cont.)
- The range of a signed integer in a byte is
therefore, -128 to127, remembering that the high
bit does not count. - The range of a signed integer in a word is
32,768 to 32,767.
25The Ones Complement
- The ones complement of a binary number is when
all digits are reversed in value. - For example,
- 0 0 0 1 1 0 1 1
- has a ones complement of
- 1 1 1 0 0 1 0 0
26Storage of Numbers
- Unsigned numbers and positive signed numbers are
stored as described above. - Negatively signed numbers, however, are stored in
a format called the Twos complement which
allows it to be added to another number as if it
was positive.
27Storage of Numbers (cont.)
- The Twos Complement of a number is obtained by
adding 1 to the lowest bit of the ones
complement of the number. - The Twos Complement is perfectly reversible
- TC (TC (number)) number.
28Storage of Numbers (cont.)
- Therefore, if the high bit is set (to 1), the
number is a negatively signed integer. - But, its decimal value can only be obtained by
taking the twos complement, and then converting
to decimal. - If the high bit is not set ( 0), then the number
can be directly converted into decimal.
29Example
- 0 0 0 0 1 0 1 0 is a positive number, as the
high bit is 0. - 0 0 0 0 1 0 1 0 can be easily converted to 10
decimal in a straightforward fashion. - 0 0 0 0 1 0 1 0 10 decimal
30Example (cont.)
- 1 0 0 0 1 0 1 0 is a negative number because of
the high bit being set. - 1 0 0 0 1 0 1 0, however, is not 10, as we first
have to determine its twos complement.
31Example (cont.)
- Ones Complement of (1 0 0 0 1 0 1 0) ? (0 1 1 1
0 1 0 1), - add 1 ? (0 1 1 1 0 1 1 0)
- ? 64 32 16 4 2 -118
32Character Representation ASCII
- How do we represent non-numeric characters as
well as the symbols for the decimal digits
themselves if we want to get an alphanumeric
combination? - Typically, characters are represented using only
one byte minus the high bit (7-bit code).
33Character Representation ASCII (cont.)
- Bits 00h to 7Fh represent the possible values.
The ASCII table maps the binary number designated
to be a character with a specific character. The
back inside cover of the textbook contains that
mapping. - If the eighth bit is used (as is done in the IBM
PC to extend the mapping to Greek and graphics
symbols), then the hex numbers used are 80h to
FFh.
34Character Representation ASCII (cont.)
- The programmer has to keep track of what a binary
number in a program stands for. - It is not inherent in the hardware or the
operating system. - High level languages do this by forcing you to
declare a variable as being of a certain type. - Different data types have different lengths
35EEL 3801
- Part II
- System Architecture
36Components
- Video Display Terminal self explanatory
- Keyboard self-explanatory
- Disk Drives self-explanatory
- System Unit contains the motherboard or the
system board. Otherwise self-explanatory
37Components (cont.)
- Random Access Memory (RAM) Electronic memory
where the program and the data are kept while the
program is running. It is volatile since the
contents are lost if there is loss of power.
Additionally, it is also called dynamic since its
contents must be continuously refreshed.
38Components (cont.)
- Read-Only Memory (ROM) BIOS Contains the
information on the input output peripherals. - CMOS RAM Keeps system setup information.
- Expansion slots Permit expansion of the system
by adding special purpose boards such as modems,
communication cards, etc.
39Components (cont.)
- Power Supply Self-explanatory
- Parallel Port Output port that transfers a set
of bits simultaneously. Typically used for
printers. Allow for quick transfer of data but
only for short distances. - Serial port Output port where single bits are
produced one by one. Slower, but useful for
longer distances.
40Components (cont.)
- Microprocessor Intel microprocessors are
downwardly compatible with each othe. - Programs written on older versions will run on
the newer ones, but programs written for the
newer versions will not run on the older ones. - Read Section 2.1 of the textbook for more details.
41System Architecture
- The Central Processing Unit (CPU) is the most
important part of the computer. It consists of
the Arithmetic logic Unit (ALU) and the Control
Unit (CU). - The ALU carries out arithmetic, logic and
shifting operations. - The CU fetches data and instructions and decodes
addresses for the ALU.
42System Architecture (cont.)
- Additionally, there may be a math coprocessor,
which speeds up mathematical calculations, as
well as many other support chips. However, they
are all coordinated by the CPU.
43The CPU
- The most basic tasks of the CPU are
- Find and load the next instruction from memory.
- Execute the instruction. This is composed of
several sub-instructions that we will discuss
later.
44The CPU (cont.)
- The CPU, besides the ALU and CU, is composed of
several other components - Data bus Wires that move data within the CPU
itself. - Registers High-speed memory elements within the
CPU itself on which can significantly speed up
the performance of the computer. - Clock A timing device whose ticks coordinate all
individual operations that take place in the
computer. These ticks are called machine cycles.
45Registers
- Registers are special work areas inside the CPU
that can store data and/or instructions. - These memory elements are very fast.
- There are several registers on the Intel 8088
family of microprocessors - Data registers
- Segment registers
- Index registers
- Special registers
- Flag register
46Data Registers
- Also called general purpose registers.
- Are used for arithmetic and data manipulation
operations. - Can be addressed as either 8 or 16 bit values, or
as both. - The 80386 and newer CPUs use 32-bit registers
addressable as 16-bit ones.
47Data Registers (cont.)
- There are several of these.
- The AX Register the accumulator register is used
by the CPU for arithmetic operations. - It is a 16-bit register, but can be addressed as
two independent 8-bit registers called AH (for
high) and AL (for low).
48Data Registers (cont.)
- The BX Register the base register is also
general purpose (like the AX). - Has the ability to hold addresses for other
variables (pointers). - Also 16-bit that can be independently addressed
as two 8-bit bytes (BH and BL).
49Data Registers (cont.)
- The CX register the counter register best serves
as the counter for repeating looping
instructions. - These instructions automatically repeat and
decrement the CX register, and quit when it
equals 0. - Also 16-bit that can be independently addressed
as two 8-bit bytes (CH and CL).
50Data Registers (cont.)
- The DX Register the data register is also
general purpose but has a special role when doing
multiplication or division.
51Segment Registers
- These registers are used to store memory
locations of either instructions or data in main
memory. - These registers contain the base segment of the
memory location where the memory segment begins.
52Segment Registers (cont.)
- There are several of these
- The CS Register the code segment register
contains the base location of the executable
instructions that make up the program. - Note that the base location can only address the
initial location where these instructions can be
found, not the entire segment..
53Segment Registers (cont.)
- The DS Register the data segment register is the
default base location in memory for variables - The SS Register the stack segment register
contains the base location of the run-time stack.
- The ES Register the extra register is an
additional memory location where additional base
locations can be stored.
54Index Registers
- Contain the offset (the distance from the base
segment) where a specific variable or instruction
may be found. The base segment and the offset
can uniquely identify any addressable location of
any length in memory. Base segment offset
memory location.
55Index Registers (cont.)
- There are several of these
- The SI Register the source index takes name from
the instruction used to move strings. - SI usually contains an offset from the DS
register, but can address any variable.
56Index Registers (cont.)
- The DI Register generally acts as a destination
for string movement instructions. Typically
contains an offset for the ES register, but not
necessarily so. - The BP Register the base pointer register
contains an offset from the stack register (SS).
- Used to locate variables in the stack.
57Special Registers
- Do not fit into any other categories.
- The IP Register the instruction pointer register
contains the offset of the next instruction to be
executed. - Combines with CS to form the complete address of
the next executable instruction.
58Special Registers (cont.)
- The SP Register the stack pointer register
contains the offset from the beginning of the
stack segment to the top of the stack. - SS and SP combine to form the complete address
for the top of the stack.
59Flags Register
- One single 16-bit register whose individual bit
positions serve as flags to indicate the status
of the CPU or the result of some arithmetic
operation. - The individual positions are predefined, although
not all 16 are defined.
60Flags Register (cont.)
- Bit positions and flags
- 0 ? Carry flag Set when result of unsigned
arithmetic operation is too large to fit into
destination. Values are 1carry 0no carry. - 1 ? undefined
- 2 ? Parity flag reflects the number of bits that
are set in the result of an operation. Can be
even or odd.
61Flags Register (cont.)
- 3 ? undefined
- 4 ? Auxiliary carry set when operation causes a
carry from bit 3 to bit 4. Rarely ever used. - 5 ? undefined.
- 6 ? Zero flag Set when result of an operation
results in zero. Used in jumping to other
instructions based on comparison of two values.
Has a value of 1when 0 0 when 0.
62Flags Register (cont.)
- 7 ? Sign flag Set when result of an operation
results in negative number. Value is 1 when
negative 0 when positive. - 8 ? Trap flag Determines whether or not the CPU
will be halted after each instruction is
executed. Allows Trace or stepping through a
programs execution. Allows the programmer to
control the CPU in this way through the INT 3
instruction.
63Flags Register (cont.)
- 9 ? Interrupt flag Makes it possible for
external interrupts to occur. Interrupts can be
disabled by setting this flag to 0. Controlled
by the programmer through the CLI and STI
instructions. - A ? Direction flag controls the assumed
direction used by the string processing
instruction. Values are 1up 0down.
Programmer can control this flag through the STD
and CLD instructions.
64Flags Register (cont.)
- B ? Overflow flag Like the Carry flag, but for
signed arithmetic operations. Value is
1overflow 0no overflow. - C, D, E and F ? undefined
65The Run-Time Stack
- The run-time stack is an important element in the
execution of a stored program. - It is a temporary holding area for addresses and
data. - It resides in the stack segment identified in the
SS and SP registers. - Each cell in the stack is 16 bits.
66Run-Time Stack (cont.)
- The stack pointer holds the last element to be
added or pushed into the stack. - This is also the first element to be taken off
the stack, or popped. - This is referred to as Last-In-First-Out (LIFO).
67The Run-Time Stack (cont.)
- There are three typical uses for the run-time
stack - If we want to save the contents of a register,
the stack makes a great place to store their
values temporarily.
68The Run-Time Stack (cont.)
- When a subroutine is called from another part of
the program, it is important that the processor
return to the place where the function was called
after it exits. The address of the instruction
that called the subroutine is saved on the stack
so as to be able to return to it later.
69The Run-Time Stack (cont.)
- Local variables can be created when a subroutine
is active and then popped off the stack when the
subroutine returns to the calling instruction.
This is done in an area inside the run-time stack
called the stack frame.
70The Run-Time Stack (cont.)
- Operations
- The push operation Used to put values of data or
instructions onto the stack. There is only on
place in the stack into which things can be
inputted the top of the stack. - mov ax,00A5 move 00A5 into AX
- push ax pushes content of ax into stack
- push bx assume BX has a value of 0001
- push cx assume cx has a value of 0002
71The Run-Time Stack (cont.)
- The push instruction does not change the value of
the source register (typically the ax register,
but could be others). Rather it simply copies
its value to the top of the stack.
72The Run-Time Stack (cont.)
- The pop operation Used to remove the value in
the stack pointed to by the stack pointer and
places it in a register or memory location
(variable). Immediately upon removing the
element popped, the SP moves to the immediately
previous element in the stack. - pop ax pops stack and puts value into AX
73The Run-Time Stack (cont.)
- Note that the value remains in the stack, but not
being pointed by the stack pointer, it is subject
to be overwritten by the next push operation.
74Microinstructions
- Machine level instructions are not the lowest
level instructions in the computer.
Microinstructions are. These are very low-level
operations that carry out the machine-level
instructions.
75Microinstructions (cont.)
- There are three basic ones
- fetch the control unit fetches the instruction,
copies it into the CPU (register). - decode this operation decodes the instruction as
well as any operands specified by the
instruction. If any operands, the control unit
fetches the operand from main memory.
76Microinstructions (cont.)
- execute the ALU executes the operation and
passes the result operands to the CU, where they
are returned to the registers and/or to main
memory. - Get next instruction
- Go back to step 1
- Microcode is the interface between the binary
code level and the electronic level.
77Memory organization of DOS
- The Intel 8086 processor can access 1 Mb of
memory (actually, 1,048,576 bytes, which is FFFFF
in a 20-bit address). This is called the Real
Mode. - The main memory is divided into RAM and ROM.
- RAM occupies low memory, and starts at 00000h and
continues up to BFFFFh.
78Memory organization of DOS (contd)
- ROM occupies high memory and begins at C0000 and
continues to FFFFF. - This is mostly used for the ROM BIOS (the hard
disk controller). - The BIOS contains diagnostic and configuration
software, as well as input-output subroutines.
79Memory organization of DOS (contd)
- Addresses begin with a hex address of 00000 and
continue incrementally until FFFFF. - DOS allows only the first 640kB of RAM to be used
for programs. - This is misleading because DOS (74kB) itself has
to occupy this area as well. - Remaining RAM used by video display and hard disk
controller.
80System Memory (cont.)
- The 80286 and more notably, the 80386 and 80486
processors can run in Protected Mode. - This means that they can radically increase the
amount of memory they can address (16MB).
81System Memory (cont.)
- The Pentium can address significantly more than
that. - Unfortunately, DOS can only run in real mode.
- However, Windows runs in protected mode and
liberates the programmer from the 1MB memory
limit.
82System Memory (cont.)
- The 80386 and beyond processor also has the
virtual 8086 mode, which allows concurrent real
mode processes to be executed in by a single CPU. - The total memory being used can total more than
the available RAM. The processor uses external
memory (hard disk drive or floppy) to page
currently unused portions of the program to these
devices.
83Address Calculations
- An address is a number that refers to an 8-bit
(byte) memory location. - The addresses are numbered consecutively,
starting at 00000h and going up to the highest
location in memory, depending on the amount of
memory available.
84Address Calculations (cont.)
- Addresses can be expressed in one of two ways
- A 32-bit (16 16) segment-offset address. This
combines a base location (the base segment) with
the offset to represent the actual address. For
example, 08F10100, where 08F1 is the base
location (segment) from which to start counting,
and 0100 is the offset, or how much to count.
The address points to the first byte in the
address.
85Address Calculations (cont.)
- A 20 bit absolute address, which refers to an
exact memory location. For example, F405Bh. - Using 20 bits, the processor can only address 1
Mb (actually, 1,048,576 bytes) of memory.
86Address Calculations (cont.)
- But address registers are only 16 bits wide,
limiting the addressable memory to 65,535. - Thus, the segment-offset technique is used to
expand the range of accessible memory beyond the
65,535 limit. - Thus, when addressing memory locations, the
registers combine the values of two registers,
the base segment and the offset.
87Address Calculations (cont.)
- The CPU uses the segment and offset value to
generate an absolute address. It adds the
segment and the offset to create the absolute
address. The segment value is always known to
have an implied half-byte at the right (0000). - Example Given an address such as 08F10100.
The absolute address (20 bit) would be calculated
as follows
88Address Calculations (cont.)
- Segment value plus implied byte 0 8 F 1 0 h
- Add the offset value 0 1 0 0 h
- __________________________________________
- Absolute Address 0 9 0 1 0 h
- The advantages to the segment offset method is
that it allows the program to be loaded into any
segment address in memory without having to
recalculate the addresses of all variables.
89Address Calculations (contd)
- Furthermore, large data structures that occupy a
large block of memory can be easily accessed by
knowing their base segment and offset.
90EEL 3801
- Part III
- Assembly Language Programming
91Assembly Language Programming
- The basic element of an assembly program is the
statement. - Two types of statements
- Instructions executable statements that actually
do something. - Directive provide information to assist the
assembler in producing executable code. For
example, create storage for a variable and
initialize it.
92Assembly Programming (contd)
- Assembly language instructions equate one-to-one
to machine-level instructions, except they use a
mnemonic to assist the memory. - Program control Control the flow of the program
and what instructions to execute next (jump,
goto). - Data transfer Transfer data to a register or to
main memory. - Arithmetic add, subtract, multiply, divide, etc.
93Assembly Programming (contd)
- Logical gt lt etc.
- Input-output read, print etc.
94Statements
- A statement is composed of a name, a mnemonic,
operands and an optional comment. Their general
format is as follows - name mnemonic operand(s) comments
95Names, labels
- Name Identifies a label for a statement or for
a variable or constant. - Can contain one or more of several characters
(see page 56 of new textbook). - Only first 31 characters are recognized
- Case insensitive.
- First character may not be a digit.
96Names, labels
- The period . may only be used as the first
character. - Cannot use reserved names.
- Can be used to name variables. Such when put in
front of a memory allocation directive. Can also
be used to define a constant. For example - count1 db 50 a variable (memory allocation
directive db) - count2 equ 100 a constant
97Names, labels
- Can be used as labels for statements to act as
place markers to indicate where to jump to. Can
identify a blank line. For example - label1 mov ax,10
- mov bx,0
- .
- .
- .
- label2
- jmp label1 jump to label1
98Mnemonics
- Mnemonic identifies an instruction or
directive. These were described above. - The mnemonics are standard keywords of the
assembly language for a particular processor. - You will become familiar with them as time goes
on this semester.
99Operands
- Operands are various pieces of information that
tells the CPU what to take the action on.
Operands may be a register, a variable, a memory
location or an immediate value. - 10 ? immediate value
- count ? variable
- AX ? register
- 0200 ? memory location
- Comments Any text can be written after the
statement as long as it is preceded by the .
100Elements of Assembly Language for the 8086
Processor
- Assembler Character Set These are used to form
the names, mnemonics, operands, variables,
constants, numbers etc. which are legal in 8086
assembly. - Constant A value that is either known or
calculated at assembly time. May be a number or
a string of characters. Cannot be changed at run
time.
101Elements of Assembly Language (cont.)
- Variable A storage location that is referenced
by name. A directive must be executed
identifying the variable name with the location
in memory. - Integers Numeric digits with no decimal point,
followed by the radix mentioned before (e.g., d,
h, o, or b). Can be signed or unsigned.
102Elements of Assembly Language (cont.)
- Real numbers floating point number made up of
digits, a decimal point, an optional exponent,
and an optional leading sign. - ( or -) digits.digits exponential ( or -)
digits
103Elements of Assembly Language (cont.)
- Characters and strings
- A character is one byte long.
- Can be mapped into the binary code equivalent
through the ASCII table, and vice-versa. - May be enclosed within single or double quotation
marks. - Length of string determined by number of
characters in string, each of which is 1 byte.
For example
104Elements of Assembly Language (cont.)
- a 1 byte long
- b 1 byte long
- stack overflow 14 bytes long
- abc?A 8 bytes long
105Example of Simple Assembly Program
- The following simple program will be demonstrated
and explained - mov ax,5 move 5h into the AX register
- add ax,10 add 10h to the AX register
- add ax,20 add 20h to the AX register
- mov sum,ax store value of AX in variable
- int 20 end program
106Example (cont.)
- The result is that at the end of the program, the
variable sum, which exists somewhere in memory
(declaration not shown), now accepts the value
accumulated in AX, namely, 35h. - Explain program.
107Example (cont.)
- Note that several things have been left off, such
as directives. However, this program captures
the essence of assembly language programming at
its simplest. - It still needs a directive to start the program.
This is the A directive. It tells the processor
to assemble this program at a particular memory
location. - A 100h
108More Complex Assembly Language Example Program
- The following is a more complex program, used to
advance an old dot matrix printer equivalently to
the formfeed command on the printer control panel.
109More Complex Assembly Language Program (cont.)
- a begin assembly
- mov ah,5 moves 5 to the AH register
- mov dl,C moves 0Ch to the DL register
- int 21 Calls DOS function 5
- int 20 terminate program
- n page.com names the program page.com
- r cx sets CX to the programs length
- the length is 8 bytes
- w writes program to disk
- q quit debug
110More Complex Example (cont.)
- The value of 5 in AH is the name of the function
to be called by the DOS subroutine. This
particular one, the DOS function 5, sends the
content of the DL register to the printer. - The w command in DEBUGGER starts writing to
memory from offset 100, up to the length of the
program. Therefore, it needs to know the length
of the program being written to disk. It counts
8 bytes (mov, ah,5,mov,dl,C,int 21, and int 20).
111More Complex Example (cont.)
- A more complex assembly program is shown below.
- This is similar to the program that you will do
in the first lab session. It is the famous C
program hello world.
112More Complex Example (cont.)
- 1 title Hello World Program (hello.asm)
- 2
- 3 this program displays Hello, World
- 4
- 5 dosseg
- 6 .model small
- 7 .stack 100h
- 8
- 9 .data
- 10 hello_message db Hello, World!,0dh,0ah,
- 11
113More Complex Example (cont.)
- 12 .code
- 13 main proc
- 14 mov ax,_at_data
- 15 mov ds,ax
- 16
- 17 mov ah,9
- 18 mov dx,offset hello_message
- 19 int 21h
- 20
- 21 mov ax,4C00h
- 22 int 21h
- 23 main endp
- 24 end main
114More Complex Example (cont.)
- The program is explained as follows
- Line 1 Contains the title directive. All
characters located after the title directive are
considered as comments, even though the symbol
is not used. - Line 3 Comment line with the symbol
- Line 5 The dosseg directive specifies a standard
segment order for the code, data and stack
segments. The code segment is where the program
instructions are stored. The data segment is
where the data (variables) are stored. The stack
segment is where the stack is maintained.
115More Complex Example (cont.)
- Line 6 The .model directive indicates the memory
architecture to be used. In this case, it uses
the Microsoft small memory architecture. It
indicates this by the word small after .model. - Line 7 This directive sets aside 100h of memory
for the stack. This is equivalent to 256 bytes
of memory (162 256). - Line 9 The .data directive marks the beginning
of the data segment, where the variables are
defined and memory allocated to them.
116More Complex Example (cont.)
- Line 10 The db directive stands for define
byte, which tells the assembler to allocate a
sequence of memory bytes to the data that follow.
0dh is a carriage return and 0ah is the linefeed
symbol. The is the required terminator
character. The number of memory bytes is
determined by the data themselves. Hello_message
is the name of the variable to be stored in
memory, and the db allocates memory to it in the
size defined by the data following it. - Line 12 The directive .code is the indication of
the beginning of the code segment. The next few
lines represent instructions.
117More Complex Example (cont.)
- Line 13 The proc directive is used to declare
the main procedure called main. Any name could
have been used, but this is in keeping with the
C/C programming requirement that the main
procedure be called the main function. The first
executable instruction following this directive
is called the program entry point - the point at
which the program begins to execute.
118More Complex Example (cont.)
- Line 14 The instruction mov is used to copy the
contents of one address into the other one. The
first operand is called the destination address,
while the second one is called the source
address. In this particular use, we tell the
assembler to copy the address of the data segment
(_at_data) into the AX register. - Line 15 Copies the content of AX into DS, which
is a register used to put the data segment, the
default base location for variables.
119More Complex Example (cont.)
- Line 17 This instruction places the value 9 in
the AH register. Remember that from the page.com
program, this is the register used to store the
name of the DOS subroutine to be called with the
int 21 instruction. - Line 18 This instruction places the address of
the string to be identified in the DX register.
Remember that this is the offset, where the
default base segment is already identified in the
DS register as the base segment for the data
segment. Since the address of the variable
hello_message begins at the beginning of the data
segment, identified by DS, we only need to supply
the offset for the variable.
120More Complex Example (cont.)
- Line 19 The instruction int 21, as we saw
before, takes the name of the function from the
DX register, which in this case is 9. DOS
funtion 9, incidentally, sends the contents of DX
register to the VRT output device. The DX
register contains the address of the string to be
sent. - Line 21 and 22 These instructions represent the
equivalent of an end or stop statement. This is
different from that done for page.com because
this will be an executable program (.exe), rather
than a .com program. More on this later. - Line 23 Indicates the end of the main procedure.
121More Complex Example (cont.)
- Line 24 The END directive the last line to be
assembled. The main next to it indicates the
program entry point.
122More Complex Example (cont.)
- The program may seem overly complicated for such
a simple program. - But remember that assembly language corresponds
one-to-one with machine language instructions. - Note that it takes only 562 bytes of memory when
assembled and compiled.
123More Complex Example (cont.)
- The same program written in a high level language
will require several more machine level
instructions to carry out the same thing. - Written in Turbo C, the executable program
would take 8772 bytes of memory to store.
124Specifics of ALP Data Definition Directives
- A variable is a symbolic name for a location in
memory. This is done because it is easy to
remember variables, but not memory locations. It
is like an aka, or a pseudonym.
125Data Definition Directives
- Variables are identified by labels. A label
shows the starting location of a variables
memory location. A variables offset is the
distance from the beginning of the data segment
to the beginning of the variable.
126Data Definition Directives (cont.)
- A label does not indicate the length of the
memory that the variable takes up. - If a string is being defined, the label offset is
the address of the first byte of the string (the
first element of the string). - The second element is the offset 1 byte.
- The third element is offset 2 bytes.
127Data Definition Directives (cont.)
- The amount of memory to be allocated is
determined by the directive itself.
128Define Byte
- Allocates storage for one or more 8-bit values
(bytes). Has the following format - name DB initialvalue ,initialvalue
- The name is the name of the variable. Notice
that it is optional.
129Define Byte (cont)
- initialvalue can be one or more 8-bit numeric
values, a string constant, a constant expression
or a question mark. - If signed, it has a range of 127 to 128, If
unsigned, it has a range of 0 255.
130Define Byte - Example
- char db A ASCII character
- signed1 db -128 min signed value
- signed2 db 127 max signed value
- unsigned1 db 0 min unsigned value
- unsigned2 db 255 max signed value
131Define Byte (cont)
- Multiple values A sequence of 8-bit numbers can
be used as initialvalue. They are then grouped
together under a common label, as in a list. - The values must be separated by commas.
- list db 10,20,30,40
132Define Byte (cont)
- The 10 would be stored at offset 0000
- 20 at offset 0001
- 30 at offset 0002 and
- 40 at offset 0003,
- where 0001 represents a single byte.
133Define Byte (cont)
- A variables value may be left undefined. This
can be done by placing a ? for each byte to be
allocated (as in a list). - count db ?
134Define Byte (cont)
- A string may be assigned to a variable, each of
whose elements will be allocated a byte. - c_string db This is a long string
- The length of a string can be automatically
determined by the assembler by the symbol. - See page 65 of new book for details.
135Define Byte Example
- Using DEBUGGER, variable names cannot be used
- A 150 Assemble data at offset 150
- db 10,0 define 2 data bytes, 1st10, 2nd0
- A 100 Assemble code at offset 100
- mov ax,0 clears the AX register
- mov ah,150 move cont. of 1st mem. addr. to AH
- add ah,10 add 10 to the contents of AH
- mov 151,ah move cont. of AH to other variabl
- int 20 end program
136Define Byte Example (cont)
- If we dump the contents of memory locations at
offset 150 and 151, we will find 10 in 150 and
20 in 151.
137Define Word
- Serves to allocate memory to one or more
variables that are 16 bits long. Has the
following format - name DW initialvalue ,initialvalue
- The name is the name of the variable. Notice
that it is optional. - initialvalue can be one or more 16-bit numeric
values, a string constant, a constant expression
or a question mark.
138Define Word (cont)
- If signed, it has a range of 32,767 to 32,768,
- If unsigned, it has a range of 0 65,535.
139Define Word (Example)
- var dw 1,2,3 defines 3 words
- signed1 dw -32768 smallest signed value
- signed2 dw 32767 largest signed value
- unsigned1 dw 0 smallest unsigned value
- unsigned2 dw 65535 largest signed value
- var-bin dw 1111000011110000b
- var-hex dw 4000h
- var-mix dw 1000h,4096,AB,0
140Reverse Storage Format
- The assembler reverses the bytes in a word when
storing it in memory. - The lowest byte is placed in the lowest address.
- It is re-reversed when moved to a 16-bit
register. - value dw 2AB6h
- B6 2A
- See example on page 50 of textbook
141Define Doubleword DD
- Same as DB and DW, except the memory allocated
is now 4 bytes (2 words, 32 bits). - name DD initialvalue ,initialvalue
- The name is the name of the variable. Notice
that it is optional. - initialvalue can be one or more 32-bit numeric
values, either in dec., hex or bin. form, string
const., a const. Expression, or ?
142Multiple Values
- A sequence of 32-bit numbers can be used as
initialvalue. - They are then grouped together under a common
label, as in a list. - The values must be separated by commas.
143Reverse Order Format
- As in define word, the bytes in doubleword are
stored in reverse order as in DW. - For example,
- var dd 12345678h
- 78 56 34 12
144Type Checking
- When a variable is created, the assembler
characterizes it according to its size (i.e.,
byte, word, doubleword, etc.). - When a variable is later referenced, the
assembler checks its size and only allows values
to be stored that come from a register or other
memory that matches in size. - Mismatched movements of data not allowed.
145Data Transfer Instructions mov
- The instruction mov is called the data transfer
instruction. - A very important one in assembly - much
programming involves moving data around. - Operands are 8- or 16-bit on the 8086, 80186 and
80286. - Operands on the 80386 and beyond, they can also
be 32-bits long.
146Data Transfer Instructions mov (cont)
- The format is
- mov destination,source
- The source and destination operands are
self-explanatory. - The source can be an immediate value (a
constant), a register, or a memory location
(variable). It is not changed by the operation. - The destination can be a register or a memory
location (variable).
147Operands
- Register Operands Transfer involving only
registers. It is the fastest. - Source any register
- Destination any register except CS and IP
- Immediate Operands
- Immediate value can be moved to a register (all
but IP) or to memory. - Destination must be of same type as source.
148Operands (cont.)
- Direct operands
- A variable may be one of the two operands, but
not both. It does not matter which is the
variable.
149Limitations on operands
- There are some limitations on mov
- CS or IP not destination operand
- Moving immediate data to segment registers.
- Moving from segment register to segment register.
- Source and destination operands of different
types. - Immediate value as destination (obviously!!)
- Memory to memory moves
150Sequential Memory Allocation
- Memory for variables is allocated sequentially by
the assembler. - If we call DB several times, such as in
- var1 db 10
- var2 db 15
- var3 db 20
151Sequential Memory Allocation (cont)
- var1 will be the first byte in the data segment
of main memory. - This segment may be identified by the base
segment and the offset. - var2 will occupy the next available memory
location, or 1 byte away from the beginning of
the data segment in memory.
152Sequential Memory Allocation (cont)
- var 3 will be 2 bytes away from this starting
point. - This will be the case even if the memory
locations are not labeled, such as in - db 10
- db 20
- db 30
153Offsets
- Many times, memory will be allocated, but not
labeled. - This is typical of an array, when only the entire
array is labeled, not each cell. - The address of the array is the address of the
first element (position) of the array. - All subsequent cells are allocated by adding an
offset to the address of the head element.
154Offsets
- This is also true when a list of elements is
defined through DB, DW, or DD. - Example an array or list of 8-bit numbers whose
memory location is called a-list. - To access the first element of a-list, we
reference the location in memory corresponding to
a-list. - To access any of the other elements of the array,
we provide an offset to the address of array. - The second element at array1, the third at
array2, the fourth at array3, etc.
155Offsets
- To move the value of the 5th element of the array
to register AL - mov al array4
- The size of the two operands must match.
- Otherwise, an error may result.
- Note that AL is used, not AX - 1 byte.
- See example on page 54 of textbook.
156PTR Operator
- Used to clarify an operands type. NOT a
pointer. - It can be placed between the mov command and the
operands, as for example Used for readability
only. It does not change the size of the operand
in any way. - mov word ptr count,10
- mov byte ptr var2,5
157XCHG Instruction
- Allows the direct exchange of values between 2
registers or between a register and a memory
location. - Very fast, used for sorting
- Needs to obey size constraints
- Used in sorting.
158Stack Operations
- Already discussed what a stack is.
- Each position in the stack is 2 bytes long only
16-bit registers can be copied into the stack. - The bottom of the stack is in high memory, and
it grows downward. - Typically, 256 bytes are allocated to the stack,
enough for 128 entries.
159Stack Operations (cont)
- Identified by the SS register (stack segment),
which identifies the address of the base location
of the stack segment. - The stack pointer (SP register) indicates the
address of the first element of the stack (top of
the stack).
160Push Operation
- Puts something (a 16-bit element) at the top
position of the stack. - Decrements stack pointer (it grows downward).
- Can put the contents of a register or of memory
(a variable) - In 80286 and later processors, it can also place
an immediate value.
161Push Operation
- Has the following form
- push ax
- push ar1
- push 1000h
162Pop Operation
- The opposite of the push operation.
- Removes the top element in the stack.
- Copies value of top element in stack to
destination indicated in the statement. - Increments stack pointer.
- Registers CS (code segment) and IP (instruction
pointer) cannot be used as operands.
163Pop Operation
- Has the following form
- pop ax
- pop ar2
164PUSHF and POPF
- Special instructions that move and remove the
contents of the Flags register onto and out of
the stack. - These are used to preserve the contents of these
registers in case it is changed and the old
values are to be reinstated. - See page 56.
165PUSHA (80186) and PUSHD (80386)
- Pushes the contents of the registers AX, CX, DX,
BX, original SP, BP, SI, and DI on the stack in
this exact order. - PUSHD does the same for 32-bit registers.
166POPA and POPD
- Pops the same registers in the reverse order.
167Arithmetic Instructions
- Form the heart of any pro