CSCI263 Computer Organization and Architectures Lecture Note Some

  • Slides: 16
Download presentation
CSCI-263 Computer Organization and Architectures Lecture Note: Some slides and/or pictures in the following

CSCI-263 Computer Organization and Architectures Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson & Hennessy, © 2005 Some slides and/or pictures in the following are adapted from: slides © 2008 UCB

The SPIM Simulator SPIM is a simulator that let you run and debug MIPS

The SPIM Simulator SPIM is a simulator that let you run and debug MIPS assembler programs. The simulator allows you to look at the content of registers and memory, and to single step through the simulation. Install PCSpim http: //www. cs. wisc. edu/~larus/spim. html Documentation book: appendix A 9 (on CD) www. cs. wisc. edu/~larus/SPIM/spim_documentation. pdf Webinterface: http: //cgi. aggregate. org/cgi-bin/cgispim. cgi

Steps to Starting a Program (translation) C program: foo. c Compiler Assembly program: foo.

Steps to Starting a Program (translation) C program: foo. c Compiler Assembly program: foo. s Assembler Object(mach lang module): foo. o Linker Executable(mach lang pgm): a. out Loader Memory lib. o

Assembly Language (cont. ) Pseudo-instructions: extending the instruction set for convenience. Examples: move $2,

Assembly Language (cont. ) Pseudo-instructions: extending the instruction set for convenience. Examples: move $2, $4 Translates to: add $2, $4, $0 # $2 = $4, (copy $4 to $2) li $8, 40 addi $8, $0, 40 # $8 = 40, (load 40 into $8) sd $4, 0($29) sw $4, 0 ($29) sw $5, 4($29) # mem[$29] = $4; Mem[$29+4] = $5 la $4, 0 x 1000056 c lui $4, 0 x 1000 ori $4, 0 x 056 c # Load address $4 = <address>

MIPS Pseudo Copy -instructions Arithmetic Shift Logic Memory access Control transfer Pseudoinstruction Usage Move

MIPS Pseudo Copy -instructions Arithmetic Shift Logic Memory access Control transfer Pseudoinstruction Usage Move Load address Load immediate Absolute value Negate Multiply (into register) Divide (into register) Remainder Set greater than Set less or equal Set greater or equal Rotate left Rotate right NOT Load doubleword Store doubleword Branch less than Branch greater than Branch less or equal Branch greater or equal move la li abs neg mul div rem sgt sle sge rol ror not ld sd blt bgt ble bge regd, regs regd, address regd, anyimm regd, regs regd, reg 1, reg 2 regd, reg 1, reg 2 regd, address reg 1, reg 2, L reg 1, reg 2, L

The MIPS Instruction Set Copy Arithmetic Logic Memory access Control transfer Instruction Usage Load

The MIPS Instruction Set Copy Arithmetic Logic Memory access Control transfer Instruction Usage Load upper immediate Add Subtract Set less than Add immediate Set less than immediate AND OR XOR NOR AND immediate OR immediate XOR immediate Load word Store word Jump register Branch less than 0 Branch equal Branch not equal Jump and link System call lui rt, imm add rd, rs, rt sub rd, rs, rt slt rd, rs, rt addi rt, rs, imm slti rd, rs, imm and rd, rs, rt or rd, rs, rt xor rd, rs, rt nor rd, rs, rt andi rt, rs, imm ori rt, rs, imm xori rt, rs, imm lw rt, imm(rs) sw rt, imm(rs) j L jr rs bltz rs, L beq rs, rt, L bne rs, rt, L jal L syscall op fn 15 0 0 0 8 10 0 0 12 13 14 35 43 2 0 1 4 5 3 0 32 34 42 36 37 38 39 8 12

Recalling Register Conventions Registers and data sizes in MIPS.

Recalling Register Conventions Registers and data sizes in MIPS.

SPIM/MIPS assembly directives. data start data segment . ascii "str" store the string "str"

SPIM/MIPS assembly directives. data start data segment . ascii "str" store the string "str" in memory without '' . asciiz "str" idem, with '' . byte 3, 4, 16 store 3 byte values . double 3. 14, 2. 72 store 2 doubles . float 3. 14, 2. 72 store 2 floats . word 3, 4, 16 store 3 32 -bit quantities . space 100 reserve 100 bytes . text start text segment © PG/HC Programming 5 JJ 70 pg 8

SPIM syscall examples Service Trap code print_int $v 0 = 1 $a 0 =

SPIM syscall examples Service Trap code print_int $v 0 = 1 $a 0 = integer to prints $a 0 to standard output print_float $v 0 = 2 $f 12 = float to prints $f 12 to standard output print_double $v 0 = 3 $f 12 = double to prints $f 12 to standard output print_string $v 0 = 4 $a 0 = address of first character prints a character string to standard output read_int $v 0 = 5 integer read from standard input placed in $v 0 read_float $v 0 = 6 float read from standard input placed in $f 0 read_double $v 0 = 7 double read from standard input placed in $f 0 read_string $v 0 = 8 $a 0 = address to place string, $a 1 = max string length reads standard input into address in $a 0 sbrk $v 0 = 9 $a 0 = number of bytes required $v 0= address of allocated memory Allocates memory from the heap exit $v 0 = 10 print_char $v 0 = 11 read_char $v 0 = 12 file_open $v 0 = 13 $a 0 = full path (zero terminated string with no line feed), $a 1 = flags, $a 2 = UNIX octal file mode (0644 for rw-r--r--) $v 0 = file descriptor file_read $v 0 = 14 $a 0 = file descriptor, $a 1 = buffer address, $a 2 = amount to read in bytes $v 0 = amount of data in buffer from file (-1 = error, 0 = end of file) file_write $v 0 = 15 $a 0 = file descriptor, $a 1 = buffer address, $a 2 = amount to write in bytes $v 0 = amount of data in buffer to file (-1 = error, 0 = end of file) file_close $v 0 = 16 $a 0 = file descriptor Input Output $a 0 = character (low 8 bits) $v 0 = character (no line feed) echoed © PG/HC Programming 5 JJ 70 pg 9

Let’s try. text. globl main: ori $t 0, $0, 0 x 2 ori $t

Let’s try. text. globl main: ori $t 0, $0, 0 x 2 ori $t 1, $0, 0 x 3 addu $t 2, $t 0, $t 1 # $8 OR(0, 0 x 2) # $9 OR(0, 0 x 3) # $10 ADD($t 0, $t 1)

Memory Map in MIPS 80000000 Overview of the memory address space in MIPS.

Memory Map in MIPS 80000000 Overview of the memory address space in MIPS.

n: m: r: . data. word 0 x 2. word 0 x 3. space

n: m: r: . data. word 0 x 2. word 0 x 3. space 4. text. globl main: la lw addu la sw $t 5, n # load address of n to $t 5 $t 0, 0($t 5) # load n to $t 0 $t 5, m # load address of m to $t 5 $t 1, 0($t 5) # load m to $t 1 $t 2, $t 0, $t 1 # $10 ADD($8, $9) $t 5, r # load address of r to $t 5 $t 2, 0($t 5) # store $10 to r

n: m: r: . data. word 0 x 2. word 0 x 3. space

n: m: r: . data. word 0 x 2. word 0 x 3. space 4. text. globl main: lw lw addu sw $t 0, n # load n to $t 0 $t 1, m # load m to $t 1 $t 2, $t 0, $t 1 # $10 ADD($8, $9) $t 2, r # store $10 to r

System calls – print_str str: . data. asciiz “Hello World”. text. globl main: li

System calls – print_str str: . data. asciiz “Hello World”. text. globl main: li $v 0, 4 la $a 0, str syscall # code for print_str # argument # executes print_str

System calls – read _int. data num: . space 4. text. globl main: li

System calls – read _int. data num: . space 4. text. globl main: li $v 0, 5 syscall la $t 0, num sw $v 0, 0($t 0) # code for read_int # executes read_int # return value is stored in $v 0 # load address of num to $t 0 # store the number in num

SPIM example 1: add two numbers # # # $t 2 $v 0 $a

SPIM example 1: add two numbers # # # $t 2 $v 0 $a 0 - used to hold the sum of the $t 0 and $t 1. - syscall number, and syscall return value. Assembler directive - syscall input parameter. starts with a dot . text # Code area starts here li $v 0, 5 syscall move $t 0, $v 0 # read number into $v 0 # make the syscall read_int # move the number read into $t 0 li $v 0, 5 syscall move $t 1, $v 0 # read second number into $v 0 # make the syscall read_int # move the number read into $t 1 main: add $t 2, $t 0, $t 1 move $a 0, $t 2 li $v 0, 1 syscall li $v 0, 10 syscall # end of main Special SPIM instruction: system call # move the number to print into $a 0 # load syscall print_int into $v 0 # # syscall code 10 is for exit # © PG/HC Programming 5 JJ 70 pg 16