130 likes | 253 Vues
This lecture focuses on key concepts in MIPS assembly language using the SPIM simulator. Students will explore assembler directives for defining data structures, such as arrays, and initializing them. The discussion will cover pseudo-instructions that simplify coding, with examples of control flow instructions including branches and loops. Understanding the assembly representation of C++ constructs, like conditionals and loops, will enhance students’ ability to write efficient MIPS code. This lecture requires completion of Homework 4 due on 10/05/04.
E N D
COMS 361Computer Organization Title: Instructions Date: 9/30/2004 Lecture Number: 11
Announcements • Homework 4 • Due 10/05/04
Review • SPIM • MIPS simulator • SPIM program (exit.s) • Comments • Assembler directives • .text • .data • Labels • Instructions • Data • MIPS DataPath • Pseudo Instructions
Outline • SPIM • MIPS simulator • Assembler directives
Allocate space in the data segment of the SPIM memory model Allocates 4096 bytes in the data segment Assembler Directives • Used to create data structures that are available at run-time • To allocate a one-dimensional array in C++ int ARRAY[1024]; • Corresponds to the MIPS assemble directive .data ARRAY: .space 4096
Assembler Directives • Allocate and initialize a one-dimensional array int ARRAY[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 }; • Corresponds to the MIPS assemble directive .data ARRAY: .word 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 la $a0 ARRAY # $a0 = &ARRAY[0] lw $s0 8($a0) # $s0 = MEM[2] $a0 contains a pointer to the array
Assembler Directives • String literal definition .data helloStr: .ascii “Hello, World!\n” • helloStr is the memory location of the array of characters • The string is not null terminated • Can cause problems (hell01.s) .data helloStr: .asciiz “Hello, World!\n”
Pseudo Instructions • Give the illusion of a richer instruction set • Translated into one or more “real” MIPS instructions la $a0 label • The problem in implementing this instruction is that label should be a 32-bit address • It will not fit into a single instruction lui $a0 upper 16-bits of label ori $a0 lower 16-bits of label
Control Flow • MIPS conditional branch instructions bne $t0, $t1, Label beq $t0, $t1, Label • Example • C/C++ code: if(i==j) { h=i+j; } • MIPS code: bne $s0, $s1, Label add $s3, $s0, $s1 Label: .... • Implemented using reverse logic • Turned the equality condition around
Control Flow if(i != j) { h = i + j; }else { h = i - j; } beq $s4, $s5, Lab1 add $s3, $s4, $s5 j Lab2 Lab1: sub $s3,$s4,$s5 Lab2: … Turned the test around Branched over the then part
Control Flow • beq, bne are useful • What about branch-if-less-than? • New instruction if($s1 < $s2) $t0 = 1; else $t0 = 0; • Vanilla MIPS does not provide a ble instruction • It can be synthesized from other MIPS instructions
Control Flow • The ble pseudo-instruction is implemented as two “real” MIPS instructions • slt $at, $s1, $s2 (set $t0 to 1 if $s1 < $s2) • beq $at, $0, Target if ($t0 == $0) goto Target0 • The assembler needs an extra register to convert this pseudo instruction into a sequence of actual MIPS instructions • $at can NOT contain a currently needed value
Example • Implement the following while loop in MIPS assembly language • Assume an array save has 10 elements and they are filled with integer values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 int j = 0, sum = 0; int save[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; while (j < 10) { sum += save[j++]); } cout << “save array sum: “ << sum << endl;