Sunteți pe pagina 1din 8

EE2801 -- Lecture 5

CPU Operations and


the Meaning of Numbers

EE2801-L05P01

What A Computer Does


Although computers can be programmed to do very complex tasks, they really operate on a very simple
cycle.

This cycle does the following:

1.

Fetch an instruction from memory

2.

Fetch 0 or more operands from memory

3.

Execute the instruction inside the processor

4.

Store 0 or more results to memory

5.

Return to step 1

Consider the following picture:

The memory contains


two kinds of values:
program instructions,
Address Bus
CPU

Memory

and program data.

They both look exactly


the same!
Data Bus

EE2801-L05P02

A Closer Look at Memory


There are two sets of numbers that are of concern when considering memory. The first, is

address, of the desired information in memory. The second is the actual


content, or data, at that address. The data may be either program instructions, or program
the location, or

data to you, but to the processor its all just data. In fact, even addresses are nothing
special - they are just a collection of bits!
Memory
Addresses

a15

Address Bus
CPU

Data Bus

a0

FFFF FFFF
FFFF FFFE
FFFF FFFD

0000
0000
0000
0000
0000
0000
0000
d7

0006
0005
0004
0003
0002
0001
0000

Data

A9
C6
9D
XX
XX
XX
19
FF
3F
2D
34
09
A7

Its the order in which


addresses are generated
that and the data at
those addresses that
ultimately determines
what the processor does.

d0

EE2801-L05P03

Processor Registers And Memory


Most of the operations that a processor does are related to memory.

Consider the following problem: C = A + B.

In C/C++ we could write this as:

int main(int argc, char* argv[])


{
char A = 7;
// Reserve space for variables.
char B = 12;
char C;
C = A + B;
}

// Implement equation.

return 0;

To execute this, the processor must:

Get A from memory and put it in a temporary register (tempa).


Get B from memory and put it in a temporary register (tempb).
Add A and C and store the result in a temporary register (tempc).
Store the content of tempc to memory.

EE2801-L05P04

A First Look At Assembly Code


If I compile this in Visual Studio and look at the disassembled code in the debugger, I find
something that looks like the following:

9:
char
00401028 mov
10:
char
0040102C mov
11:
char
12:
13:
C = A +
00401030 movsx
00401034 movsx
00401038 add
0040103A mov

A = 7; // Reserve space for variables.


byte ptr [ebp-4],7
B = 12;
byte ptr [ebp-8],0Ch
C;
B;

// Implement equation.
eax,byte ptr [ebp-4]
ecx,byte ptr [ebp-8]
eax,ecx
byte ptr [ebp-0Ch],al

In this example, the 004010XX numbers are addresses of instructions, mov is an assembly
language instruction for moving data, epb is a CPU register used as a pointer, eax, ecl, and
al are additional internal processor registers.

Note:

A and B are stored at ebp-4 and ebp-8 respectively.


C is not initialized to anything!
A gets stored into eax
B gets stored into ecx
A + B gets stored into eax, overwriting the previous content
al gets stored at epb - 12.

EE2801-L05P05

EAX, EBP, ECX, EEEEK, Whats All That?

EE2801-L05P06

A Working 8086 Assembly Language Program


Now lets rewrite our assembly language program using straight ahead, 8-bit 80x86
assembly language instructions:

A
B
C

.Model
.Stack

small
;64K maximum memory size.
100h ;Reserve 256 bytes for the stack.

.Data
db 7d
db 12d
db ?

;Mark the start of the


;Initialize space and value
;Initialize space and value
;Initialize space and value

.Code
.8086

data space.
for variable A.
for variable B.
for variable B.

;Mark start of code segment.


;Force the generation of 8086 compatible code.

start:
nop
;Entry point for program.
;First, the data segments must be initialized so that the data is accessible.

stop:

mov

ax,@data

;Move the data segment address


;into the AX register.
;Transfer data segment address
;into the segment register.

mov

ds,ax

mov
mov
add
mov

al,byte ptr[A] ;Load register with variable A.


bl,byte ptr[B] ;Load register with variable B.
al, bl
;al = al + bl
byte ptr[C], al
;Store result in memory

jmp

stop

END start
EE2801-L05P07

The Output Of The Assembler


Turbo Assembler
lec5add.ASM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Version 4.0

0000
0000
0000
0000
0001
0002

.Model
.Stack
07
0C
??

0003

01/19/00 20:42:02

small
100h

Page 1

;64K maximum memory size.


;Reserve 256 bytes for the

stack.

.Data
A db
B db
C db

;Mark the start of the data space.


7d
;Initialize space and value for variable A.
12d
;Initialize space and value for variable B.
?
;Initialize space and value for variable B.

.Code
.8086

;Mark start of code segment.


;Force the
generation of 8086 compatible
;code.

0000

90

start: nop
;Entry point for program.
;First, the data segments must be initialized so that the data is
;accessible.

0001

B8 0000s

mov

ax,@data

0004

8E D8

mov

ds,ax

0006
0009
000D
000F

A0
8A
02
A2

mov
mov
add
mov

al,byte ptr[A] ;Load register with variable A.


bl,byte ptr[B] ;Load register with variable B.
al, bl
;al = al +
bl
byte ptr[C], al ;Store result in memory

0012

EB FE

0000r
1E 0001r
C3
0002r

stop:

;Move the
;into the
;Transfer
;into the

jmp

data segment address


AX register.
data segment address
segment register.

stop
EE2801-L05P08

S-ar putea să vă placă și