CS 230 - Spring 2020 2-1
Computer Systems Lecture 7 Assembly Language Intro CS 230 - Spring - - PowerPoint PPT Presentation
Computer Systems Lecture 7 Assembly Language Intro CS 230 - Spring - - PowerPoint PPT Presentation
CS 230 Introduction to Computers and Computer Systems Lecture 7 Assembly Language Intro CS 230 - Spring 2020 2-1 System Layers Python/C/Racket Code Multiprocessing and Operating Systems Build and Runtime Environments Memory and
System Layers
CS 230 - Spring 2020 2-2
Transistors and Electrical Properties Logic Gates Binary Signals & Number Representation CPU Instructions and Pipelining Memory and Caching Build and Runtime Environments Multiprocessing and Operating Systems Python/C/Racket Code CS 230 Here now! Finished!
CS 230 - Spring 2020 2-3
Machine Code
Binary code – comprised of 0s and 1s “Direct” execution by processor Program composed of instructions
operation code (opcode) + operands instructions control processor
opcode says what thing to do operands say what to apply that thing to
CS 230 - Spring 2020 2-4
Assembly Language
Human-readable “programming language”
very simple compared to Racket, Python, etc.
Almost direct mapping to machine code
except a few concepts we’ll cover later
Assembler turns it into machine code
process is called “assembling” rather than “compiling”
CS 230 - Spring 2020 2-5
Instruction Set
Repertoire of instructions of a processor Different processors have different sets
many commonalities
mathematical memory access control flow
CS 230 - Spring 2020 2-6
MIPS Architecture
MIPS: Microprocessor without Interlocked
Pipeline Stages
details later
Multiple revisions, systems, and compilers
not just a single standard MIPS we use simplified version in CS 230
CS 230 - Spring 2020 2-7
MIPS Assembly Language
Each instruction takes 32 bits
4 bytes = 1 word
Arithmetic instructions operate on registers
32 registers available numbered $0 to $31
refer to them in assembly language with $[register number]
register $0 always equals 0
Instructions have up to 3 operands
1st is destination, 2nd and 3rd are sources same register can be source and destination
CS 230 - Spring 2020 2-8
Immediate Addition
addi $t, $s, i
add register s and value i value i can be negative value i can be hexadecimal place result in register t example: addi $1, $2, 14
sets content of $1 to the content of $2 plus 1410
often used to initialize registers: addi $t, $0, i
CS 230 - Spring 2020 2-9
Addition and Subtraction
add $d, $s, $t
add content of register s and t place result in register d example: add $3, $2, $1
sets content of $3 to content of $2 plus content of $1
sub $d, $s, $t
subtract register t from s place result in register d example: sub $6, $5, $4
sets content of $6 to content of $5 minus content of $4
Example
addi $2, $0, -3 add $3, $2, $0 sub $3, $3, $2 jr $31 What is the value in register $3 at the end of this program?
CS 230 - Spring 2020 2-10
Example
addi $2, $0, -3 add $3, $2, $0 sub $3, $3, $2 jr $31 What is the value in register $3 at the end of this program?
CS 230 - Spring 2020 2-11
$0 $1 $2 $3 $4 $5 … ? ? ? ? ? …
Example
addi $2, $0, -3 add $3, $2, $0 sub $3, $3, $2 jr $31 What is the value in register $3 at the end of this program?
CS 230 - Spring 2020 2-12
$0 $1 $2 $3 $4 $5 … ?
- 3
? ? ? …
Example
addi $2, $0, -3 add $3, $2, $0 sub $3, $3, $2 jr $31 What is the value in register $3 at the end of this program?
CS 230 - Spring 2020 2-13
$0 $1 $2 $3 $4 $5 … ?
- 3 -3
? ? …
Example
addi $2, $0, -3 add $3, $2, $0 sub $3, $3, $2 jr $31 What is the value in register $3 at the end of this program?
CS 230 - Spring 2020 2-14
$0 $1 $2 $3 $4 $5 … ?
- 3
? ? …
Example
addi $2, $0, -3 add $3, $2, $0 sub $3, $3, $2 jr $31 What is the value in register $3 at the end of this program?
- $3 = 010
CS 230 - Spring 2020 2-15
$0 $1 $2 $3 $4 $5 … ?
- 3
? ? …
Try it yourself
addi $1, $0, 13 addi $2, $1, 0xA sub $4, $2, $1 jr $31 What is the value in register $4 at the end of this program?
CS 230 - Spring 2020 2-16
Try it yourself
addi $1, $0, 13 addi $2, $1, 0xA sub $4, $2, $1 jr $31 What is the value in register $4 at the end of this program?
CS 230 - Spring 2020 2-17
$0 $1 $2 $3 $4 $5 … ? ? ? ? ? …
Try it yourself
addi $1, $0, 13 addi $2, $1, 0xA sub $4, $2, $1 jr $31 What is the value in register $4 at the end of this program?
CS 230 - Spring 2020 2-18
$0 $1 $2 $3 $4 $5 … 0 13 ? ? ? ? …
Try it yourself
addi $1, $0, 13 addi $2, $1, 0xA sub $4, $2, $1 jr $31 What is the value in register $4 at the end of this program?
CS 230 - Spring 2020 2-19
$0 $1 $2 $3 $4 $5 … 0 13 23 ? ? ? …
Try it yourself
addi $1, $0, 13 addi $2, $1, 0xA sub $4, $2, $1 jr $31 What is the value in register $4 at the end of this program?
CS 230 - Spring 2020 2-20
$0 $1 $2 $3 $4 $5 … 0 13 23 ? 10 ? …
Try it yourself
addi $1, $0, 13 addi $2, $1, 0xA sub $4, $2, $1 jr $31 What is the value in register $4 at the end of this program?
- $4 = 1010
CS 230 - Spring 2020 2-21
$0 $1 $2 $3 $4 $5 … 0 13 23 ? 10 ? …
Example
Consider the equation z = x + y + 8310 Assume x represents $1, y represents $2, and z represents $3. Write a MIPS assembly language program that evaluates the value of z given x and y.
CS 230 - Spring 2020 2-22
Example
Consider the equation z = x + y + 8310 Assume x represents $1, y represents $2, and z represents $3. Write a MIPS assembly language program that evaluates the value of z given x and y. add $3, $1, $2
CS 230 - Spring 2020 2-23
Example
Consider the equation z = x + y + 8310 Assume x represents $1, y represents $2, and z represents $3. Write a MIPS assembly language program that evaluates the value of z given x and y. add $3, $1, $2 addi $3, $3, 83
CS 230 - Spring 2020 2-24
Example
Consider the equation z = x + y + 8310 Assume x represents $1, y represents $2, and z represents $3. Write a MIPS assembly language program that evaluates the value of z given x and y. add $3, $1, $2 addi $3, $3, 83 jr $31
CS 230 - Spring 2020 2-25
Try it Yourself
Consider the equation z = (x – y) – (0xB3C + y) Assume x represents $1, y represents $2, and z represents $3. Write a MIPS assembly language program that evaluates the value of z given x and y.
CS 230 - Spring 2020 2-26
Try it Yourself
Consider the equation z = (x – y) – (0xB3C + y) Assume x represents $1, y represents $2, and z represents $3. Write a MIPS assembly language program that evaluates the value of z given x and y. sub $4, $1, $2
CS 230 - Spring 2020 2-27
Try it Yourself
Consider the equation z = (x – y) – (0xB3C + y) Assume x represents $1, y represents $2, and z represents $3. Write a MIPS assembly language program that evaluates the value of z given x and y. sub $4, $1, $2 addi $3, $2, 0xB3C
CS 230 - Spring 2020 2-28
Try it Yourself
Consider the equation z = (x – y) – (0xB3C + y) Assume x represents $1, y represents $2, and z represents $3. Write a MIPS assembly language program that evaluates the value of z given x and y. sub $4, $1, $2 addi $3, $2, 0xB3C sub $3, $4, $3
CS 230 - Spring 2020 2-29
Try it Yourself
Consider the equation z = (x – y) – (0xB3C + y) Assume x represents $1, y represents $2, and z represents $3. Write a MIPS assembly language program that evaluates the value of z given x and y. sub $4, $1, $2 addi $3, $2, 0xB3C sub $3, $4, $3 jr $31
CS 230 - Spring 2020 2-30