Einführung in MIPS Programmierung, Skripte von Rechnerarchitektur

Unvollständige Textvorschau

Nur auf Docsity: Lade Einführung in MIPS Programmierung und mehr Skripte als PDF für Rechnerarchitektur herunter!

Fundamentals of Computer Systems

The MIPS Instruction Set

Stephen A. Edwards and Martha A. Kim

Columbia University

Spring 2012

Instruction Set Architectures MIPS The GCD Algorithm MIPS Registers Types of Instructions Computational Load and Store Jump and Branch Other Instruction Encoding Register-type Immediate-type Jump-type Assembler Pseudoinstructions

Higher-Level Constructs Expressions Conditionals Loops Arrays Strings & Hello World ASCII Subroutines Towers of Hanoi Example Factorial Example Memory Layout Differences in Other ISAs

Machine, Assembly, and C Code

00010000100001010000000000000111 00000000101001000001000000101010 00010100010000000000000000000011 00000000101001000010100000100011 00000100000000011111111111111100 00000000100001010010000000100011 00000100000000011111111111111010 00000000000001000001000000100001 00000011111000000000000000001000

beq $4, $5, 28 slt $2, $5, $ bne $2, $0, 12 subu $5, $5, $ bgez $0 - subu $4, $4, $ bgez $0 - addu $2, $0, $ jr $

Machine, Assembly, and C Code

00010000100001010000000000000111 00000000101001000001000000101010 00010100010000000000000000000011 00000000101001000010100000100011 00000100000000011111111111111100 00000000100001010010000000100011 00000100000000011111111111111010 00000000000001000001000000100001 00000011111000000000000000001000

beq $4, $5, 28 slt $2, $5, $ bne $2, $0, 12 subu $5, $5, $ bgez $0 - subu $4, $4, $ bgez $0 - addu $2, $0, $ jr $

gcd: beq $a0, $a1, .L slt $v0, $a1, $a bne $v0, $zero, .L subu $a1, $a1, $a b gcd .L1: subu $a0, $a0, $a b gcd .L2: move $v0, $a j $ra

Algorithms

al · go · rithm

a procedure for solving a mathematical problem (as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operation; broadly : a step-by-step procedure for solving a problem or accomplishing some end especially by a computer

Merriam-Webster

The Stored-Program Computer

John von Neumann, First Draft of a Report on the EDVAC ,

“Since the device is primarily a computer, it will have to perform the elementary operations of arithmetics most frequently. [...] It is therefore reasonable that it should contain specialized organs for just these operations.

“If the device is to be [...] as nearly as possible all purpose, then a distinction must be made between the specific instructions given for and defining a particular problem, and the general control organs which see to it that these instructions [...] are carried out. The former must be stored in some way [...] the latter are represented by definite operating parts of the device. “Any device which is to carry out long and complicated sequences of operations (specifically of calculations) must have a considerable memory.

Architecture vs. Microarchitecture

Architecture: The interface the hardware presents to the software

Microarchitecture: The detailed implemention of the architecture

MIPS

M icroprocessor without I nterlocked P ipeline S tages MIPS developed at Stanford by Hennessey et al. MIPS Computer Systems founded 1984. SGI acquired MIPS in 1992; spun it out in 1998 as MIPS Technologies.

The GCD Algorithm

Euclid, Elements , 300 BC.

The greatest common divisor of two numbers does not change if the smaller is subtracted from the larger.

  1. Call the two numbers a and b
  2. If a and b are equal, stop: a is the greatest common divisor
  3. Subtract the smaller from the larger
  4. Repeat steps 2–

The GCD Algorithm

Let’s be a little more explicit:

  1. Call the two numbers a and b
  2. If a equals b , go to step 8
  3. if a is less than b , go to step 6
  4. Subtract b from a a > b here
  5. Go to step 2
  6. Subtract a from b a < b here
  7. Go to step 2
  8. Declare a the greatest common divisor
  9. Go back to doing whatever you were doing before

Euclid’s Algorithm in MIPS Assembly

gcd: beq $a0, $a1, .L2 # if a = b, go to exit sgt $v0, $a1, $a0 # Is b > a? bne $v0, $zero, .L1 # Yes, goto .L

subu $a0, $a0, $a1 # Subtract b from a (b < a) b gcd # and repeat

.L1: subu $a1, $a1, $a0 # Subtract a from b (a < b) b gcd # and repeat

.L2: move $v0, $a0 # return a j $ra # Return to caller Operands: Registers, etc.

Euclid’s Algorithm in MIPS Assembly

gcd: beq $a0, $a1, .L2 # if a = b, go to exit sgt $v0, $a1, $a0 # Is b > a? bne $v0, $zero, .L1 # Yes, goto .L

subu $a0, $a0, $a1 # Subtract b from a (b < a) b gcd # and repeat

.L1: subu $a1, $a1, $a0 # Subtract a from b (a < b) b gcd # and repeat

.L2: move $v0, $a0 # return a j $ra # Return to caller Labels

Euclid’s Algorithm in MIPS Assembly

gcd: beq $a0, $a1, .L2 # if a = b, go to exit sgt $v0, $a1, $a0 # Is b > a? bne $v0, $zero, .L1 # Yes, goto .L

subu $a0, $a0, $a1 # Subtract b from a (b < a) b gcd # and repeat

.L1: subu $a1, $a1, $a0 # Subtract a from b (a < b) b gcd # and repeat

.L2: move $v0, $a0 # return a j $ra # Return to caller Arithmetic Instructions

Euclid’s Algorithm in MIPS Assembly

gcd: beq $a0, $a1, .L2 # if a = b, go to exit sgt $v0, $a1, $a0 # Is b > a? bne $v0, $zero, .L1 # Yes, goto .L

subu $a0, $a0, $a1 # Subtract b from a (b < a) b gcd # and repeat

.L1: subu $a1, $a1, $a0 # Subtract a from b (a < b) b gcd # and repeat

.L2: move $v0, $a0 # return a j $ra # Return to caller Control-transfer instructions