Start Reading Chapter 3
Some of this material is from Section 3.2.1
An Introduction to Assembly Language
- In the simplest assembly language model, a computer consists of:
- A main memory consisting of an array of bytes, consecutively
numbered start at 0.
These numbers are called memory addresses.
- A program counter, or PC, which can hold a memory address.
- A register file containing a small number of named locations.
Each location (register) can hold a fixed amount of information
corresponding to the word size of the machine.
A typical word size is 4 bytes (32 bits).
- Condition code registers which contain a small number of
bits of information.
These contain information about the last arithmetic or logical
operation.
For example, ZF (zero flag) is set if the last operation resulted
in 0 and SF (sign flag) is set if the last operation yielded a
negative value.
- A set of floating-point registers for holding floating-point
data.
- A program is stored in main memory in consecutive memory addresses.
- A program consists of instructions, each taking up 1 or more bytes
of memory.
- The PC holds the address of the next instruction to execute.
- An instruction consists of an operation, followed by 0 or more
operands.
In the simplest case, the operation is always a fixed number of bytes,
often 1 byte.
Most instructions have 1 or 2 operands.
- Registers may have special meanings, for example, one of the registers
may be designated as a stack pointer and used for push and pop operations.
- A program is executed as follows:
- Fetch the operation at the location of the PC.
- Increment the PC to point past the operation.
- Decode the operation and determine how many operands and
how big (number of bytes) each operands is.
- Fetch the operands, incrementing the PC to point past each operand
as it is fetched.
- Execute the instruction.
- Do it again
- An operand typically represents one of the following:
- A register
- An immediate value (a small number stored with the instruction)
- A memory address
- A memory address may be specified:
- by a register containing the address
- the sum of two registers
- the sum of a register and an immediate value: called base register
with displacement
- Many instructions have source and destination operands.
We will write the source first and the destination second.
Today's News: February 12
Exam one week from today!
Revised exam info available
here
- Some example types of instructions:
- Move:
rrmov rA, rB: register to register
irmov V, rB: immediate to register
rmmov ra, D(rB): register to memory, base register with displacement
mrmov D(rB), rA: memory to register
- Arithmetic operations: (integer)
add rA, rB: add rA and rB and store the result in rB, set the condition flags
sub rA, rB
and rA, rB
- Call and return:
call Dest
ret
- Push and Pop:
push rA
pop rA
- Jump instructions:
jmp Dest (always jump)
je Dest (jump if the last operation set the ZF flag)
jge Dest
Question:
What does the following program do?
mrmov 12(r1), r2
mrmov 8(r1), r3
add r2, r3
rmmov r3, 8(r1)
Answer:
Implements x = x + y
Base register plus displacement addressing:
Why is base register plus displacement addressing important?
How C functions are implemented (simplified)
Consider the following function:
int sum(int x, int y) {
return x + y;
}
How this works:
- Before the function is entered, a stack is set up with the stack pointer
contained in a designated register.
- The stack grows toward low memory.
- The stack pointer points to the last item pushed on the stack
- The values of x and y are pushed on the stack.
- The return address is also pushed on the stack.
- Assume r1 is the stack pointer and all items are 4 bytes.
- the return address is at 0(r1)
- x is at 4(r1)
- y is at 8(r1)
Question:
What is located at 0(r1)?
Answer: