Document 15817844

advertisement
Good Luck (:
Please go to the correct room based off of your discussion section: @546
9:00 - 11:00 (53705) BUR 116
11:00 - 1:00 (53710) BUR 212
1:00 - 3:00 (53715) BUR 216
***If you plan on using your laptop, don’t forget to download your projects from the lab
computers. Master Gheith says he might reference them directly and ask about
implementation.***
***Also, since there will be a limit on the amount of available connections in the room,
be nice and bring a multiple outlet connector(for someone else and you)***
Chapter 1:
Hardware
●
Busses: Carry bytes of information between components. Usually transfer fixed size
chunks called words. The number of bytes in a word varies across systems.
● I/O devices: Systems connected to the external world. Each device is connected by an
adaptor or controller (which transfers stuff between I/O device and I/O bus)
○ ex: keyboard, mouse, display, disk drive
○ Adaptor: card that plugs into a spot on the motherboard
○ Controller: chip that sets in the device or on the motherboard
● Main Memory: A temporary storage device that holds both the program and the data it
manipulates. Logically, it’s a linear array of bytes.
● Processor: Engine that interprets/executes instructions stored in main. Inside it has the
Program Counter which at any point in time contains the address of some machine
language instruction from main memory.
Register File is a small storage device that consists of all collections of word sized
registers each with a unique name.
ALU computes new data/address value.
● Things CPU does:
○ load: copies a byte/word from memory to a register
○ store: copies a byte/word from a register to memory
○ operate: lets ALU do its thing with contents from two registers and copies the
result into a register.
TLDR: A computer system consists of hardware and software that cooperate to run applications
programs. Information inside the computer is represented as groups of bits that are interpreted
in different ways, depending on the context. Programs are translated by other programs into
different forms, beginning as ASCII text and then translated by compilers and linkers into binary
executable files.
Chapter 2: Main points
2.2 and 2.3 -refer to @447
Convert Hex to binary, binary to hex, decimal to binary, binary to decimal
Big & Small Endian - 0x01234567
Mem Location
0x100
0x101
0x102
0x103
Big Endian
0x01
0x23
0x45
0x67
Little Endian
0x67
0x45
0x23
0x01
Decimal
Hex
Binary
0
0
0000
1
1
0001
2
2
0010
3
3
0011
4
4
0100
5
5
0101
6
6
0110
7
7
0111
8
8
1000
9
9
1001
10
A
1010
11
B
1011
12
C
1100
13
D
1101
14
E
1110
15
F
1111
Boolean Identities:
The boolean operators are:
~ not
. and
+ or
Duality Principle:
If a statement is true then its dual statement is also true. The dual statement is obtained by:
(1) adding explicit parentheses
(2) exchanging "0" <-> "1" and "." <-> "+"
(3) Make sure to not flip inverters. i.e. A’ does not go to A
Law of double complement
~(~a) = a
Complement law
x + ~x = 1
x . ~x = 0
Idempotent law
a+a=a
a.a=a
Identity law
0+a=a
1.a=a
Dominance law
1+a=1
0.a=0
Commutative law
a+b=b+a
a.b=b.a
Associative law
a + ( b + c) = (a + b) + c
a . (b . c) = (a . b) . c
Distributive law
a + (b . c) = (a + b) . (a + c)
a . (b + c) = (a . b) + (a . c)
Absorption law
a + (a . b) = a
a . (a + b) = a
Simplification law
a + ~a . b = a + b
a . (~a + b) = a . b
De-Morgan's law
~(a + b) = ~a . ~b
~(a . b) = ~a + ~b
Chapter 3: Main points
Assembly commands, programming in assembly,
● 8 registers:
○ Caller saved- When procedure B is called by procedure A, it can overwrite the
register without destroying data required by A
■ %eax, %ecx, %edx
○ Callee saved- Must save the values of these registers on the stack before
overwriting them; then restore before returning
■ %ebx, %esi, %edi
○ Stack management
■ %esp, %ebp
○ example: %eax
●
Set up Stack:
○ push %ebp
mov %esp, %ebp
……
mov %ebp, %esp
pop %ebp
●
Access 1st element in linked list (32 bits), put pointer to first in edx, place first element in
ecx, and then place pointer to next node in edx (example):
○ mov 4(%esp), %edx
mov 4(%edx), %ecx
mov 0(%edx), %edx
●
Data in memory:
○ .data
○ x: .fill 255, 4, 0
○ y: .long 0
○ ect…
■ “.text” for instructions
Chapter 4:
4.2 Logic Gates
AND gate
OR gate
A B
x
0
0
0
0
1
0
1
0
0
1
1
1
A B
x
0
0
0
0
1
1
1
0
1
1
1
1
a
x
0
1
1
0
NOT gate
Combinatorial logic:
S0
Z
0
A
1
B
Multiplexer (mux)
Boolean formula: (A and not S) or (B and S)
Half adder
Boolean formula:
S = A xor B
C = A and B
A B
S C
0
0
0
0
0
1
1
0
1
0
1
0
1
1
0
1
A B Cin
S Cout
0
0
0
0
0
Full adder
0
0
1
1
0
Boolean formula:
S = A xor B xor C
0
1
0
1
0
Cout = (B and C) or (A and not (B or Cin))
0
1
1
0
1
1
0
0
1
0
1
0
1
0
1
1
1
0
0
1
1
1
1
1
1
R S
Q /Q
0
0
Q /Q
0
1
1
0
1
0
0
1
1
1
X X
The two Xs are actually 0 and 0. But this
serves no purpose as Q and Q bar only make
sense when opposite.
SR latch
Demux
X
A/Enable
Output
0
0
0
1
0
0
0
1
Y2
1
1
Y1
Exclusive OR (XOR)
A
B
Output
0
0
0
0
1
1
1
0
1
1
1
0
Assembly x86-32
Data Type
Number of Bytes
char
1
word
2
long
4
Instruction
Details
MOVL <src>, <dest>
Used to move 32 bits worth of information to
a destination that is exactly 32 bits in size
MOVB <src>, <dest>
Used to move a byte of information into a
register that is exactly 1 byte in size
MOVW <src>, <dest>
Used to move 2 bytes of information to a
destination that is exactly 2 bytes in size
MOVXYZ <src>, <dest>
Used to perform command “X” after moving
“Y” to “Z”
X = command
Y = source’s data type
Z = destination’s data type
‘s’ for “X” will cause a sign extension
‘z’ for “X” will cause a zero extension
ADDL <src>, <dest>
Adds the source to the destination and stores
the new sum in the destination. Also changes
flags.
SUBL <src>, <dest>
Subtracts the source from the destination and
stores the new value in the destination. Also
changes the flags.
PUSH <register>
Pushes a value onto the stack. If the register
is not 4 bytes, the assembler will pad the
necessary bytes with zeroes in order to make
it 4 bytes.
Below is another way of representing a push
SUB $4, %ESP
MOV <src>, (%ESP)
POP <register>
Pops a value off the stack. The amount that is
popped off the stack will be 4 bytes no matter
what. (Assuming we’re working with a 32 bit
architecture)
Below is another way of representing a pop
MOV (%ESP), <dest>
ADD $4, %ESP
Note: Moving the value into a register is not
required.
SHR *amount*, <src> - Logical
SAR *amount*, <src> - Arithmetic
Shifts the bits *amount* times to the right
Divides the src by 2^n, where n is the number
of bits shifted
SHL *amount*, <src> - Logical
SAL *amount*, <src> - Arithmetic
Shifts the bits *amount* times to the left
Multiplies the src by 2^n, where n is the
number of bits shifted
LEA
Stands for Load Effective Address.
Ex. LEA (%EDX, %EAX, 4), <dest>
This will move the MEMORY ADDRESS that
is given from the computation of:
<dest> = %EDX + (%EAX * 4)
into <dest>
Ex. MOV (%EDX, %EAX, 4), <dest>
This will move the VALUE in the memory
address that is given from the computation of:
<dest> = %EDX + (%EAX * 4)
into <dest>
XOR <src>, <dest>
Stands for Exclusive Or.
Will compare corresponding bits and will
output 0 if they are the same and 1 otherwise.
Ex. <src>: 11101101
<dest>: 00010110
Output:
11111011
INC <dest>
DEC <dest>
increment: dest++
decrement: dest--
ADD <src>, <dest>
SUB <src>, <dest>
MUL num(%reg) - Unsigned
IMUL num(%reg) - Signed
DIV num(%reg) - Unsigned
IDIV num(%reg) - Signed
add: dest += src
subtract: dest -= src
multiply: it’s complicated
AND <src>, <dest>
OR <src>, <dest>
NOT <dest>
NEG <dest>
bitwise AND: dest &= src
bitwise OR: dest |= src
bitwise NOT: dest = ~dest
compute two’s complement: dest = -dest
CMPL (Long)
CMPW (Word)
CMPB (Byte)
Compares the number of bytes depending on
the instructions and sets the correct flags.
division: it’s complicated
Used before conditional jumps and moves.
CF: (unsigned) t < (unsigned) a
ZF: (t == 0)
SF: (t < 0)
0F (a < 0 == b < 0) && (t < 0 != a < 0)
SET %8bitReg
Flags:
Unsigned overflow
Zero
Negative
Signed Overflow
SETE
SETNE
SETS
SETNS
SETG
SETGE
SETL
SETLE
Sets an 8-bit register (%al, etc.) to 0 or 1,
based on the flags (CF, ZF, SF, 0F)
E - Sets register to 1 if cmp is equal
NE - 1 if cmp is not equal
S - 1 if cmp is negative
NS - 1 if cmp is nonnegative
H - 1 if cmp is greater
GE - 1 if cmp if greater than or equal to
L- 1 if cmp is less
LE - 1 if cmp is less than or equal to
JMP
JE
JNE
JL
JG
JLE
JGE
Commonly used types of jumps.
E - Equal to
NE - Not equal to
L - Less than (src < dest)
G - Greater than
LE - Less than or equal to
GE - Greater than or equal to
CMOV
CMOVE
CMOVNE
Commonly used types of conditional moves.
E - Equal to
NE -Not equal to
CMOVL
CMOVG
CMOVLE
CMOVGE
L - Less than (src < dest)
G - Greater than
LE - Less than or equal to
GE - Greater than or equal to
.data
varName: .fill #ofrows, numOfBytes, numInit
Creating and accessing a .data array
.text
mov $varName, %varNameReg
mov $0, %indexReg
instruct (%varNameReg, %indexReg,
numofBytes), %reg
.data
varName: .long 0
Variable that’s stored in .data
If no instruction suffix is provided, the assembler attempts to infer the correct suffix based on
size of the operands. If this cannot be done (e.g. in case of memory accesses), the assembler
throws an error. example:
inc (%ecx) -- error - how many bytes is the value that %ecx points to?
incb (%ecx) -- ecx points to a one-byte value. increment it
●
●
●
The lea instruction does everything that the mov instruction does except for actually
getting the value from memory.
mov: dest = mem[offset + base + (index * scale)]
lea: dest = offset + base + (index * scale)
Project 0
Objective:
~~~~~~~~~~
Become familiar with git and the project submission system
Assignment:
~~~~~~~~~~~
Change "main.c" and make it print "pass" instead of "fail"
Remember to commit your changes (for example):
git commit -a -m "finished assignment"
Remember to push you commit to the server (for example):
git push
Project 1
Objective:
~~~~~~~~~~
write a simple program in x86 assembly
Assignment:
~~~~~~~~~~~
Write a program that reads characters from standard input and prints them to
standard output. If a character is contained in your CSID, its case is flipped
before printing it out, otherwise, it is printed as is.
For example, if your CSID is BOB92, the following transformations would take
places:
Hello => HellO
Goodbye => GOOdBye
On to school => on tO schOOl
Project 2
Objective:
~~~~~~~~~~
Practice your x86 assembly skills by manipulating stacks and pointers
Assignment:
~~~~~~~~~~~
You need to finish the implementation of two functions: check and reverse
You should be able to clone, compile, test, and push on either Linux or
MacOS but your final submission needs to work correctly on the CS Linux
machines.
int check(Node* head);
void reverse(Node* head);
"check" takes a single argument which is a pointer to the first node in
a linked list and returns an integer value. The returned value is either:
* -1 if the list contains a cycle (loops on itself)
* number of nodes in the list if the list is linear (doesn't contain
a cycle)
You can't make any assumptions about the number of nodes in the list.
There is a neat trick to help you find a cycle in a linked list. Please
feel free to discuss the algorithm on Piazza without sharing code. The trick was moving two
nodes at different speeds. AKA tortoise and hare algorithm
"reverse" takes a single argument which is a pointer to the first node
in a linked list and reverses the contents of the list in-place by replacing
the values in the nodes not by manipulating the pointers.
For example, if the original list contained [1, 2, 3], calling reverse on the list will move the values
around and make it contain [3, 2, 1]
Each node in the list contains 8 bytes. The first 4 bytes are a pointer to
the next node in the list and next 4 contain an integer value.
Project 3
Objective:
~~~~~~~~~~
Learn how to work with arrays and store data across function calls
Assignment:
~~~~~~~~~~~
You need to finish the implementation of an x86-32 assembly function "count"
int count(char str[]);
"count" takes a single argument which is a zero-terminated ASCII string. It
counts the number of instances of each character in the string and returns
the ASCII code of the character with the highest frequency for the entire
history of the function.
"null" (the character with ASCII code 0) doesn't count
Ties are broken by returning the ASCII code with the lowest numerical value.
For example, calling
count("xay")
returns the ASCII code for 'a'
Now, calling
count("xb")
returns the ASCII code for 'x'
Now, calling
count("by")
returns the ASCII code for 'b'
Project 4
Objective:
~~~~~~~~~~
Learn how to reason about the impact of instruction selection on performance
Deliverables:
~~~~~~~~~~~~~
You're asked to make an educated guess about the expected performance of
a program, finish its implementation, then explain the actual performance.
- edit the file named GUESS and try to guess the missing information. Your
score will not depend on your guess.
- finish the implementation
- edit the file named EXPLAIN and do your best to explain the actual results.
Assignment:
~~~~~~~~~~~
You need to finish the implementation of two x86-32 assembly functions:
"count1" and "count2"
int count1(int array[], int size, int limit);
int count2(int array[], int size, int limit);
Where:
array
size
limit
is an array of integers
specifies the number of integers in the array
is some integer value
Both count1 and count2 return the number of array elements that are less
than the given limit.
Restrictions:
~~~~~~~~~~~~~
count1 is not allowed to use the "cmov" family of instructions
count2 can have a maximum of one conditional jump in its implementation and
is required to use a "cmov" instruction in its implementation
Project 5
Objective:
~~~~~~~~~~
Learn how to develop a functional simulator
Assignment:
~~~~~~~~~~~
Finish the Java implementation of a functional simulator for a simple
instruction set architecture.
The instruction set architecture:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Our architecture has:
- 4 8-bit registers R0, R1, R2, and R3
- an 8-bit PC
- an 8-bit result register
All registers are initialized to 0.
Instructions are either 1 or 2 byte long. Here are the defined instructions:
binary encoding
instruction
description
00000000
halt
stop the processor
000011dd
jle Rdd
branch to the address in Rdd
if the value in the result
register is less than or equal
to zero
000110dd
outch Rdd
print the character whose
ASCII code is in Rdd
000111dd
show Rdd
show Rdd
print the value in Rdd as both
hexadecimal and decimal
integer
show Rdd
001000dd vvvvvvvv
movi vvvvvvvv,Rdd
move the 8 bit immediate
value vvvvvvvv to Rdd
1000ssdd
add Rss,Rdd
add the contents of Rss and
Rdd and store the result in
Rdd
1011ssdd
cmp Rss,Rdd
subtract the contents of Rss
from Rdd and store the result
in the result register
The simulator terminates if it encounters an invalid instruction
Download