Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.

Similar presentations


Presentation on theme: "CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson."— Presentation transcript:

1 CSCI-365 Computer Organization 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

2 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 http://www.cs.wisc.edu/~larus/spim.html  Documentation  book: appendix A9 (on CD)  www.cs.wisc.edu/~larus/SPIM/spim_documentation.pdf www.cs.wisc.edu/~larus/SPIM/spim_documentation.pdf  Webinterface: http://cgi.aggregate.org/cgi-bin/cgispim.cgihttp://cgi.aggregate.org/cgi-bin/cgispim.cgi

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

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

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

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

7 Recalling Register Conventions Registers and data sizes in MIPS.

8 © PG/HC Programming 5JJ70 pg 8 SPIM/MIPS assembly directives.datastart data segment.ascii "str"store the string "str" in memory without '\0'.asciiz "str"idem, with '\0'.byte 3,4,16store 3 byte values.double3.14, 2.72store 2 doubles.float 3.14, 2.72store 2 floats.word 3,4,16store 3 32-bit quantities.space 100reserve 100 bytes.textstart text segment

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

10 Let’s try.text.globl main main: ori $t0, $0, 0x2 # $8  OR(0, 0x2) ori $t1, $0, 0x3 # $9  OR(0, 0x3) addu $t2, $t0, $t1 # $10  ADD($t0, $t1)

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

12 .data n:.word 0x2 m:.word 0x3 r:.space 4.text.globl main main: la $t5, n # load address of n to $t5 lw $t0, 0($t5) # load n to $t0 la $t5, m # load address of m to $t5 lw $t1, 0($t5) # load m to $t1 addu $t2, $t0, $t1 # $10  ADD($8, $9) la $t5, r # load address of r to $t5 sw $t2, 0($t5) # store $10 to r

13 .data n:.word 0x2 m:.word 0x3 r:.space 4.text.globl main main: lw $t0, n # load n to $t0 lw $t1, m # load m to $t1 addu $t2, $t0, $t1 # $10  ADD($8, $9) sw $t2, r # store $10 to r

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

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

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


Download ppt "CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson."

Similar presentations


Ads by Google