Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

LECTURE 1

Computer: Is a machine that can be instructed to carry out sequences of arithmetic or


logical operations automatically via computer programming General purpose Programmable
Automatic With storage
Stored-program computer: A computer with a von Neumann architecture stores program
data and instruction data in the same memory, while a computer with a Harvard architecture
has separate memories for storing program and data. The memory stores data stores
programs
The Central Processing Unit must: Load instructions Interpret instructions Load data
Process data Write data
ALU Arithmetic-Logic Unit: The ALU is a “calculation” unit of the computer, providing the
calculation capacity.
CPUbuses The ALU is connected to storage elements via data buses.
The Control Unit fetches and decodes the instructions.
Instruction decoding: The Control Unit indicates to the ALU what operation must be
performed.
Memory: Two levels of memory, with their associated buses
Registers: The registers store parameters and results from operations, and control data
Basic Operation: It is only possible to execute a program stored in the main memory. CPU
performs repeatedly the following cycle: FETCH (RAM) the following instruction of the
memory to the instructions register DECODE (CU) the instruction (that is, ‘understand’ which
one it is) EXECUTE (ALU) the instruction
Registers vs Memory: Registers are faster to access than memory Operating with memory
data requires ‘loads’ and ‘stores’ More instructions to perform The compiler must use the
variables’ registers as much as possible Only store in the memory the least used variables
Optimizing registers is very important!
Interpreter: Translates one statement at a time Less amount of time to analyse the source
Overall execution time is slower No intermediate object is generated Translates the code
until an error is met Harder to guarantee correct programs Python, Ruby, …
Compiler: Scans the entire program and translates it as a whole into a different language
Large amount of time to analyse the source code code Overall execution time is faster
Generates intermediate object to be linked Generates error only after scanning the whole
program Analysis of code results in safer programs C, C++, ...
Assembly Language: Assembly language is closely related with machine language, and
normally there is an easy way of translating programs written in assembly language to
machine language. The translation algorithm is generally implemented by a program called
the assembly
CISC and RISC:
2 main types of architectures
CISC Emphasis on hardware Multiple instruction sizes and formats Less registers More
addressing modes Extensive use of microprogramming Instructions take a varying amount
of time Pipelining is difficult
RISC (Von Neumann model) Emphasis on software Instructions of the same set with few
formats Uses more registers Fewer addressing modes Complexity in compiler Instructions
take one cycle time Pipelining is easy
Computer : a machine that can be instructed to carry out sequences of arithmetic or logical
operations automatically via computer programming computer architecture : describes the
computer’s elements and the relations between them. Von Neumann architecture : CPU,
memory, peripherals, stored-program operation cycle of a computer : fetch-execute cycle.

LECTURE 2
Example: 0x3BFD 3 B F D ↔ 0011 1011 1111 1101
For negative numbers, we start from −2 n−1 and we move towards the positive side by the
natural number represented by the n − 1 bits to the right.
1000 = −2 3 = −8
1 If X is positive (or zero): 1 Convert X to base 2 2 Add leading zeros
2 If X is negative: 1 Covert |X| (the absolute value) to base 2 2 Add leading zeros 3 Negate
all the bits (swap each 0 for 1 and each 1 for 0) 4 Add 1.
37 1 Covert 3710 to base 2 : 1001012 2 Add leading zeros: 001001012 (since it is positive
we add a 0 in front, and we extend this 0 to the left until the 8 bits are filled)
−37 1 Covert X (the absolute value) to base 2: 1001012 2 Add leading zeros 001001012 3
Negate all the bits (swap each 0 for 1 and each 1 for 0) 110110102 4 Add 1. 110110112
2’s complement properties
Method to change the sign of a number that is already in 2’s complement (positive or
negative)
Negate each bit and add 1 to the result
Method to extension of a number that is already in 2’s complement
Add the extra bits (positions) with the same value as the current most significant bit.
Overflow rule: If two numbers of equal sign are added (both positive or both negative), there
is overflow if and only if, the result has opposite sign
Subtraction rule: To compute a subtraction (such as A − B), we compute a sum (A + (−B)).
That is, we find the representation as 2’s complement of −B and perform addition in binary.
The subtraction rule is the fundamental and most important reason for using 2’s
complement. The subtraction rule simplifies dramatically the design of the Arithmetic Logic
Unit.
Subtraction rule: To compute a subtraction (such as A − B), we compute a sum (A + (−B)).
That is, we find the representation as 2’s complement of −B and perform addition in binary.
The subtraction rule is the fundamental and most important reason for using 2’s
complement. The subtraction rule simplifies dramatically the design of the Arithmetic Logic
Unit.
UNSIGNED
The sum of 2 natural numbers of n bits must have n bits. If in the end there is carry, the
result occupies more than n bits, and the result is incorrect. Overflow
In this example, we add two 7-bit numbers and the result is also 7-bit. The result is correct.
In this example, we add two 4-bit numbers and the result is 5 bits. The result is wrong.
SIGNED
Adding signed numbers (2’s complement)
Consider adding 2 integer numbers in complement 2. If carry occurs in the final result of the
sum, it is eliminated. It does not mean overflow !! Example: Add 48 and -19; that is, compute
48+(-19). 19 in binary is: 00010011 -19 in binary (2’s complement) is: 11101100 +1 =
11101101
When we use a finite number of bits to represent numbers, we always run the risk that the
result of our calculations is too large to be stored in the computer (in the same number of
bits). Although we cannot always prevent overflow, we can always detect overflow.
Overflow: Therefore, we must be very careful when doing signed binary arithmetic and take
steps to detect erroneous results. If A and B are of the same sign, but the result of A + B is
of the opposite (different) sign, then an overflow error has occurred If A and B are of different
signs, then A + B will never overflow
Overflow: Carry is important when Adding or subtracting unsigned integers. It indicates that
a sum of unsigned numbers is out of bounds. Overflow is important when Adding or
subtracting signed integers. It indicates that a signed sum is out of bounds. Overflow occurs
when Adding of two positive numbers results in a negative number. Adding of two negative
numbers results in a positive number.
LECTURE 3
MIPS Instruction Set Architecture (ISA): General characteristics of MIPS ISA: 32 bits
RISC architecture 1 word = 32 bits 1 halfword =16 bits 1 byte=8 bits 1 doubleword=64 bits
Unified memory for data and instructions. Capacity for 230 words. 32 registers of 32 bits
each. Remember what we said (Lecture 1) about very few registers
MIPS memory: usage: The main memory is used for complex data Arrays (vectors),
structures, dynamic data To apply arithmetic operations Load values of the memory in
registers Store the result register value in the memory Memory is byte addressed Each
address identifies a byte of 8 bits Words are aligned in the memory The address should be a
multiple of 4
MIPS operations and operands Assembler instructions are formed by an operation code
followed by some operands Operation (instruction) What is to be executed Operand Input or
output values for an operation MIPS operations/instructions offer Arithmetic (integer/floating
point)point)( sub, Logic(and, or, Shift (shifts bits to the left or to the right) Comparison (do
something if an operand is lower than other, Load/store to transfer data from/to the memory
Branch/jump System control operations
Coding instructions CPU instructions are regular patterns usually comprising a word. The
type of instruction is associated with the format of the instructions Most instructions operate
on values in registers (even to bring data into and out of memory) Instructions represented in
binary, just like data Instructions and data stored in memory Programs can operate on
programs
LECTURE 4
.data Indicates the start of the data segment .text Indicates the start of the code segment
MIPS: Arithmetic Overflow Sum (detects overflow) add rd, rs, rt Assigns the result of the sum
of registers rs and rt to the register rd. Immediate Sum (detects overflow) addi rt, rs, imm
Assigns the result of the sum of register rs and the immediate value (16 bits sign extended )
to the register rt [Immediate value is stored within the instruction]. Subtraction (detects
overflow) sub rd, rs, rt Assigns the result of the subtraction of rs and rt to the register rd. If
the sum or subtraction produce and arithmetic overflow in 2’s complement in 32bit
representation, an exception is generated
MIPS: Arithmetic Overflow undetected with unsigned values Sum (no overflow detection)
addu rd, rs, rt Assigns the result of the sum of registers rs and rt to the register rd. Immediate
Sum (no overflow detection) addiu rd, rs, imm Assigns the result of the sum of register rs
and the immediate value (16 bits sign extended ) to the register rt [Immediate value is stored
within the instruction]. Subtraction (no overflow detection) subu rd, rs, rt Assigns the result of
the subtraction of rs and rt to the register rd. If the sum or subtraction produce and arithmetic
overflow in 2’s complement in 32bit representation, NO exception is generated and the
programmer is responsible for detecting the error
Registers vs Memory Registers are faster to be accessed than memory Operate with data
in memory requires loads and stores. Memory access is slower than register access
Optimization : Compiler must use registers to store program variables as much as possible
MIPS: logic operations (OR) OR or rd, rs, rt Evaluates the logic OR, bit by bit, of the two
registers rs and rt, and the result is stored in register rd. OR immediate ori rt, rs, imm
Evaluates the logic OR , bit by bit, of the register rs and a constant imm, and the result is
stored in register rd.
NOR nor rd, rs, rt Evaluates logic NOR, bit a bit, of both registers rs and rt and the result is
stored in the register rd XOR xor rd, rs, rt Evaluates logic XOR, bit a bit, of both registers rs
and rt and the result is stored in the register rd
Left logical shift sll rd, rt, shamt Left logical shift with a register sllv rd, rt, rs Right arithmetic
shift sra rd, rt, shamt Right arithmetic shift with a register srav rd, rt, rs Right logical shift srl
rd, rt, shamt Right logical shift with a register srlv rd, rt, rs Shift register rt (left/right) as many
bits as shamt value or rs register indicates. The result is stored in register rd.
LECTURE 5
Arrays or vectors: group of elements of the same type in one dimension Matrices: group of
elements of the same type in multiple dimensions All elements are stored in consecutive
memory addresses. Every element can be accessed using an index.
Calculation of the vector element’s address Example: Assume we have a vector declared as
int V[6] = {5, -2, 6, 9, -7, 4}; We want to access to a specific element , i.e. we want to random
access to an specific element such as V[3]. First we calculate the memory address in which
the element is stored
In MIPS You must calculate the address of the element Formula: @V1[i]=@V1+(i* size); size
is the size in bytes of an element of the array V. @V1 is the base address (start of the array
in memory)
MIPS directive for explicit alignment .align n This directive aligns the next data definition in
an address multiple of 2n . .half, .word and .dword perform automatic alignment for
addresses multiple of of 2, 4 and 8 respectively. .align 0 deactivates the automatic
alignment. Avoid using it, unless you know what you are doing.
Summary Arrays are a common data structure, all items are of the same type In assembly
language, the address of an element of an array is computed by multiplying the size in bytes
of the elements by the index and adding that to the base address of the array. The memory
is analogous to a large array of bytes. In it, different arrays may need to be allocated
(placed). Strings is not the same data type as an array of chars.

LECTURE 6
System calls (SYSCALL) MIPS has a special instruction: syscall These are calls to the OS
to perform a specific action We will use it to print data in the console / read data from the
console For using syscall 1 System (instruction) code is loaded in register $v0 2 Arguments
if needed are loaded in registers $a0, $a1, etc. 3 Execute (invoke) syscall 4 Returns values,
if they exist, in the registers
Comparison instructions: For example : slt (Set Less Than) Jump instructions: Two types
Branch: it compares the value of the operand-registers according to different conditions If the
comparison is true, it jumps to execute the given instruction Otherwise it follows executing
the next instruction (does nothing) Jump: It jumps to execute a different part of the program,
not the next instruction
JUMP instructions: Branch: Jumps to a specified instruction, if the condition is satisfied. The
address of the instruction to jump is close to the current address in the PC (relative jump of
the PC) Used for control flow instructions: loops, if-then-else (branching) Jump: Jumps to a
specified instruction unconditionally. The address of the instruction to jump is not necessarily
close to the current address in the PC. Used for procedural abstraction: subroutine calls

system call (commonly abbreviated to syscall) is the programmatic way in which a computer
program requests a service from the kernel of the operating system on which it is executed.
a while loop is a control flow statement that allows code to be executed repeatedly based on
a given Boolean condition. A branch is an instruction in a computer program that can cause
a computer to begin executing a different instruction sequence and thus deviate from its
default behaviour of executing instructions in order. A branch instruction can be either an
unconditional branch, which always results in branching, or a conditional branch, which may
or may not cause branching depending on some condition.
LECTURE 7
bytes vs word : Iterating over an array requires to carefully manipulate the index address as
per the type of the elements of the array (in MIPS increment by 1, if byte, but increment by 4
if word). branching to implement loops : at the end of all control structure programming, there
is nothing more that elementary branching operations of the machine language.
MISCELLANEOUS
The address indicated by label A is placed in $t0, thus $t0 ← 0x1001000.
• instruction address register Is a processor register that indicates where a computer is in its
program sequence. • instruction register It holds the instruction currently being executed or
decoded. • processor register A quickly accessible location available to a computer’s
processor • MIPS Microprocessor without Interlocked Pipelined Stages • Harvard
architecture Instructions and data in separate memories • control instructions Do not execute
the next PC value • load-store architecture Two categories of instructions: memory access
and ALU operations • ALU Operations on integer binary numbers • op-code instruction is
represented by a unique combination of bits • von Neumann architecture Instruction fetch
and a data operation cannot occur at the same time
: Assembly language is nothing but a direct textual representation of binary code. true The
0s and 1s that conform an instruction have a direct translation into the more humanly
readable text of assembly language.
The construct for making decisions in programming languages is usually called the if-then
control structure. true It corresponds to the diamond in flow-charts, and is where the control
flow can execute or not a block of instructions.
When dealing with arrays, indexing requires building the destination address by multiplying
the size in bytes with the index and this displacement is added to the base address, thus,
time required to retrieve an element depends on which element. false This is very close to
being true, but is false because the number of instructions to build the address is a constant
that does not depend on the index.
Pseudo-instructions are legal MIPS assembly language instructions that do not have a direct
hardware implementation. They are provided as a convenience for the programmer. When
you use pseudo-instructions in a MIPS assembly language program, the assembler
translates them into equivalent real MIPS instructions. For example, you write move $t0, $t1,
but the assembler translates this to addu $t0, $zero, $s0. true This is precisely what a
pseudo instruction is and its meaning.
addi op-code 810
010116 0000000100000001

’b’ 9810

You might also like