Telematics group University of Göttingen, Germany

Size: px
Start display at page:

Download "Telematics group University of Göttingen, Germany"

Transcription

1 Computer Science II Part 3 Assembly Language Prof. Dr. Dieter Hogrefe Dr. Xiaoming Fu Kevin Scott, M.A. Telematics group University of Göttingen, Germany Table of Content Introduction MIPS architecture CPU registers Memory map Basis of MIPS assembly language Use of labels Addressing Input/Output Instructions MIPS programming skills Control structures Stack and procedure calls How to write assembly language programs Summary 2 1

2 What is Assembly Language Assembly language is the symbolic representation of a computer s machine language More readable than coding in bits: instructions, registers, labels of memory units Directly operate hardware, using instructions defined by the CPU architecture We are going to study the assembly language for MIPS architecture (a family of RISC architecture) MIPS assembly language 3 Java/ Cprogram Compiler Translation of Programs: a Hierarchy Assembly language program High Level Language (HLL) programs first compiled (possibly into assembly), then linked and finally loaded into main memory. Assembler Compiler: translate programs in HLL into an equivalent program in machine or assembly language Assembler: translate assembly language into binary instructions. Linker: Combine a collection of object and library files into an executable file (machine language) that the computer can run Object: Machine language module Linker Object: Library routine (machine language) Executable: Machine language program Loader Memory 4 2

3 Execution Cycle Instruction Fetch Instruction Decode Operand Fetch Execute Result Store Next Instruction Load instruction from program storage Determine required actions and instruction size Locate and obtain operand data Compute result value or status Deposit results in storage for later use Determine next instruction 5 Levels of Abstraction High Level Language Program (e.g., C) Assembly Language Program (e.g., MIPS) Machine Language Program (MIPS) Datapath Transfer Specification Compiler Assembler Machine Interpretation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw $15, 0($2) lw $16, 4($2) sw$16, 0($2) sw$15, 4($2) IR Imem[PC]; PC PC + 4 ALUOP[0:3] InstReg[9:11] & MASK C code Assembly code Machine code Reasons to use HLL language? When is appropriate for using ASM language? 6 3

4 Review C Operators/Operands Operators: +, -, *, /, % (mod); (7/4==1, 7%4==3) Operands: Variables: fahr, celsius Constants: 0, 1000, -17, 15.4 In C (and most High Level Languages) variables declared and given a type first Example: int fahr, celsius; int a, b, c, d, e; 7 C Operators/Operands This is the Computation Model by ``State-Effects''. Programs move from state to state by means of assignments changing their state Assignment Statement: Variable = expression, e.g., celsius = 5*(fahr-32)/9; a = b+c+d-e; 8 4

5 Assembly Operators Syntax of Assembly Operator 1) operation by name Mnemonics'' 2) operand getting result Register or Memory 3) 1st operand for operation 4) 2nd operand for operation Ex. add b to c and put the result in a: add a, b, c Called an Assembly Language Instruction Equivalent assignment statement in C: a = b + c; 9 Assembly Operators/Instructions MIPS Assembly Syntax is rigid: 1 operation, 3 variables Why? Keep Hardware simple via regularity How to do the following C statement? a = b + c + d - e; Break into multiple instructions add a, b, c # a = sum of b & c add a, a, d # a = sum of b,c,d sub a, a, e # a = b+c+d-e To right of sharp sign (#) is a comment terminated by end of the line. Applies only to current line. C comments have format /* comment */, can span many lines 10 5

6 Assembly Operators/Instructions Note: Unlike C (and most other HLLs), each line of assembly contains at most one instruction add a,b,c add d,e,f add a,b,c add d,e,f WRONG RIGHT 11 Directives A directive is an instruction for the assembler (not the CPU) for reserving memory, telling the assembler where to place instructions, etc. E.g., in MIPS assembly language.data # data memory tmp:.word 0 # 32 bit variable.text.align 2.globl main # program memory # word alignment # main is global Here.data and.text are used by the assembler to decide where to put corresponding segments 12 6

7 Compilation How to turn the notation that programmers prefer into notation computer understands? Program to translate C statements into Assembly Language instructions; called a compiler Example: compile by hand this C code: a = b + c; d = a - e; Easy: add a, b, c sub d, a, e Big Idea: compiler translates notation from one level of computing abstraction to lower level 13 Compilation 2 Example: compile by hand this C code: f = (g + h) - (i + j); First sum of g and h. Where to put result? Add f, g, h # f contains g+h Now sum of i and j. Where to put result? Cannot use f! Compiler creates temporary variable to hold sum: t1 add t1, i, j # t1 contains i+j Finally produce difference sub f, f, t1 # f = (g+h)-(i+j) 14 7

8 Compilation -- Summary C statement (5 operands, 3 operators): f = (g + h) - (i + j); Becomes 3 assembly instructions (6 unique operands, 3 operators): add f,g,h # f contains g+h add t1,i,j # t1 contains i+j sub f,f,t1 # f=(g+h)-(i+j) In general, each line of C produces many assembly instructions One reason why people program in C vs. Assembly; fewer lines of code Other reasons? (many!) 15 Assembly Design: Key Concepts Assembly language is essentially directly supported in hardware, therefore... It is kept very simple! Limit on the type of operands Limit on the set operations that can be done to absolute minimum. if an operation can be decomposed into a simpler operation, don t include it. 16 8

9 Comments in Assembly Another way to make your code more readable: comments! Hash (#) is used for MIPS comments anything from hash mark to end of line is a comment and will be ignored Note: Different from C. C comments have format /* comment */, so they can span many lines 17 Getting Started: SPIM a MIPS Simulator SPIM is a simulator for MIPS assembly language reads a MIPS assembly language program. simulates each instruction. displays values of registers and memory supports breakpoints and single stepping provides simple I/O for interacting with user. 18 9

10 SPIM Versions SPIM is the command line version. XSPIM is X-Windows version (Unix workstations). (This is the version you will be using in the lab.) There is also a Windows version PCSpim. You can use any of these at home and they can be downloaded from: 19 SPIM Program MIPS assembly language. Must include a label main this will be called by the SPIM startup code (allows you to have command line arguments). Can include named memory locations, constants and string literals in a data segment

11 General Layout Data definitions start with.data directive Code definition starts with.text directive text is the traditional name for the memory that holds a program. Usually have a bunch of subroutine definitions and a main. 21 Simple Example.data # data memory tmp:.word 0 # 32 bit variable.text.align 2.globl main # program memory # word alignment # main is global main: lw $a0,tmp Please look at the example as shown in my PCspim window (what do you see?) 22 11

12 An Example Window in PCspim 23 Table of Content Introduction MIPS architecture CPU registers Memory map Basis of MIPS assembly language Use of labels Addressing Input/Output Instructions MIPS programming skills Control structures Stack and procedure calls How to write assembly language programs Summary 24 12

13 MIPS Architecture (Summary) Instruction Categories Load/Store Computational Jump and Branch Floating Point (coprocessor) Memory Management Special Registers R0 - R31 PC HI LO 3 Instruction Formats: all 32 bits wide R: I: J: OP Rs Rt rd sa funct OP Rs Rt Immediate OP jump target 25 Assembly Variables: Registers (1/4) Unlike HLL, assembly cannot use variables Why not? Keep Hardware Simple Assembly Operands are registers limited number of special locations built directly into the hardware operations can only be performed on these! Benefit: Since registers are directly in hardware, they are very fast 26 13

14 Assembly Variables: Registers (2/4) Drawback: Since registers are in hardware, there are a predetermined number of them Solution: MIPS code must be very carefully put together to efficiently use registers 32 registers in MIPS Why 32? Smaller is faster Each MIPS register is 32 bits wide Groups of 32 bits called a word in MIPS 27 Assembly Variables: Registers (3/4) Registers are numbered from 0 to 31 Each register can be referred to by number or name Number references: $0, $1, $2, $30, $

15 Assembly Variables: Registers (4/4) By convention, each register also has a name to make it easier to code For now: $16 - $22 $s0 - $s7 (correspond to C variables) $8 - $15 $t0 - $t7 (correspond to temporary variables) In general, use register names to make your code more readable 29 Example $0 is hardwired to value 0; it can also be written as $zero. This makes it convenient to initialize registers to specific values E.g., we can initiate the content of $t0 to the value 0 or to the value 16 as follows: add $t0, $0, $0 # initialize $t0 to the value 0 addi $t0, $0, 16 # initialize $t0 to the value 16 This is also useful to move values between registers E.g., if we wish to move the content of register $t0 to $t7: add $t7, $t0, $zero Of course, alternatively one can use move instruction: move $t7, $t

16 Memory Addresses As the purpose of cache is to transparently speed up memory access, our abstract view of memory does not show cache. Data: MIPS memory is an array of 2 32 bytes. Each byte has a 32-bit address. Each byte can hold an 8-bit data, one of the 256 possible 8-bit data. The addresses of MIPS main memory range from 0x to 0xFFFFFFFF. However, user programs and data are restricted to the first 2 31 bytes. The last half of the address space is used for specialized purposes. OPERATIONS: The CPU interacts with memory by moving data between memory and its registers. Load: a bit pattern starting at a designated address in memory is copied into a register inside the processor. Store: a bit pattern is copied from a processor register to memory at a designated address. Data are copied between the memory and the processor in groups of 1, 2, 4 or 8 contiguous bytes. Only the address of the first byte of memory is specified. 0xFFFF FFFF... 0x x x x Main memory addresses 31 MIPS Memory Map 0x x : reserved (unavailable by user programs) Text segment stores machine-level instructions starting at 0x Instructions are 32-bit words and are constrained to be stored starting on word boundaries. Data segment starts at 0x , stores data in memory. Data can be either static (e.g, char, static int) or dynamic (e.g., malloc(), free() in c). Stack segment locates below 0x7FFFFFFF. With high level languages, local variables and parameters are pushed and popped on the stack as procedures are activated and deactivated. Each segment should be 32-bit aligned (padded) 0xFFFF FFFF. 0x x7FFFFFFF 0x x x Reserved for ROM and OS Stack Segment Data Segment Text Segment Reserved MIPS memory map 32 16

17 Table of Content Introduction MIPS architecture CPU registers Memory map Basis of MIPS assembly language Use of labels Addressing Input/Output Instructions MIPS programming skills Control structures Stack and procedure calls How to write assembly language programs Summary 33 True Assembly Language MIPS (Million Instructions Per Second) Pseudo-instruction: A MIPS instruction that doesn t turn directly into a machine language instruction. This makes the MIPS assembly language to be more programmer friendly What happens with pseudo instructions? They re broken up by the assembler into several real MIPS instructions. But what is a real MIPS instruction? 34 17

18 Example Pseudoinstructions Register Move move reg2,reg1 Expands to: add reg2,$zero,reg1 Load Immediate li reg,value If value fits in 16 bits: ori reg,$zero,value else: lui reg,upper 16 bits of value ori reg,$zero,lower 16 bits 35 True Assembly Language MAL (MIPS Assembly Language): the set of instructions that a programmer may use to code in MIPS; this includes pseudoinstructions TAL (True Assembly Language): the set of instructions that can actually get translated into a single machine language instruction (32-bit binary string) A program must be converted from MAL into TAL before it can be translated into 1s and 0s

19 Labels also allow Programmer-Friendly Consider a data directives as follows:.data L1:.word 0x22 # start of data segment # therefore L1 is address 0x L2:.asciiz Print me! # Print me! only uses 9 bytes + 1byte # for null termination character.align 2 # this lets the assembler to move to # next word boundary, L3:.space 32 # thus the value of L3 is 0x Here, L1, L2 and L3 are labels. The value of a label is actually a memory address 37 Table of Content Introduction MIPS architecture CPU registers Memory map Basis of MIPS assembly language Use of labels Addressing Input/Output Instructions MIPS programming skills Control structures Stack and procedure calls How to write assembly language programs Summary 38 19

20 Assembly Operands: Memory C variables map onto registers; what about large data structures like arrays? 1 of 5 components of a computer: memory contains such data structures But MIPS arithmetic instructions only operate on registers, never directly on memory. Data transfer instructions transfer data between registers and memory: Memory to register Register to memory Addressing mode: the way of which addresses for memory access are constructed. 39 Data Transfer: Memory to Reg (1/4) To transfer a word of data, we need to specify two things: Register: specify this by number (0-31), or name ($t0..) Memory address: more difficult - Think of memory as a single one-dimensional array, so we can address it simply by supplying a pointer to a memory address. This pointer can be a label or content of a register. - Other times, we want to be able to offset from this pointer

21 Data Transfer: Memory to Reg (2/4) To specify a memory address to copy from, specify two things: A register which contains a pointer to memory A numerical offset (in bytes) The desired memory address is the sum of these two values. Example:8($t0) specifies the memory address pointed to by the value in $t0, plus 8 bytes 41 Data Transfer: Memory to Reg (3/4) Load Instruction Syntax: 1 2, 3(4) where 1) operation (instruction) name (lw, lui, etc) 2) register that will receive value 3) numerical offset in bytes 4) register containing pointer to memory Instruction Name: lw (meaning Load Word, so 32 bits or one word are loaded at a time) 42 21

22 Example: lw Data Transfer: Memory to Reg (4/4) $t0, 12($s0) This instruction will take the pointer in $s0, add 12 bytes to it, and then load the value from the memory pointed to by this calculated sum into register $t0 Notes: $s0 is called the base register 12 is called the offset offset is generally used in accessing elements of array or structure: base register points to beginning of array or structure 43 Data Transfer: Reg to Memory Also want to store value from a register into memory Store instruction syntax is identical to Load instruction syntax Instruction Name: sw (meaning Store Word, so 32 bits or one word are loaded at a time) Example: sw $t0, 12($s0) This instruction will take the pointer in $s0, add 12 bytes to it, and then store the value from register $t0 into the memory address pointed to by the calculated sum 44 22

23 Pointers vs. Values Key Concept: A register can hold any 32-bit value. That value can be a (signed) int, an unsigned int, a pointer (memory address), etc. If you write lw $t2, 0($t0) then, $t0 better contain a pointer What if you write add $t2,$t1,$t0 then, $t0 and $t1 must contain? 45 Addressing: Byte vs. word Every word in memory has an address, similar to an index in an array Early computers numbered words like C numbers elements of an array: Memory[0], Memory[1], Memory[2], Called the address of a word Computers needed to access 8-bit bytes as well as words (4 bytes/word) Today machines address memory as bytes, hence word addresses differ by 4 Memory[0], Memory[4], Memory[8], 46 23

24 Notes about Memory Forgetting that sequential word addresses in machines with byte addressing do not differ by 1. Many assembly language programmers have endured the errors made by assuming that the address of the next word can be found by incrementing the address in a register by 1 instead of by the word size in bytes. So REMEMBER that for both lw and sw, the sum of the base address and the offset must be a multiple of 4 (to be word aligned) 47 More Notes about Memory: Alignment MIPS requires that all words start at addresses that are multiples of 4 bytes Bytes in Word Aligned Not Aligned Word Location Called Alignment: objects must fall on address that is multiple of their size

25 Role of Registers vs. Memory What if more variables than registers? Compiler tries to keep most frequently used variable in registers Writing less frequently used to memory: spilling Why not keep all variables in memory? Smaller is faster: registers are faster than memory Registers more versatile: MIPS arithmetic instructions can read 2, operate on them, and write 1 per instruction MIPS data transfer only read or write 1 operand per instruction, and no operation 49 Big Idea: Stored-Program Concept Computers built on 2 key principles: 1) Instructions are represented as numbers. 2) Therefore, entire programs can be stored in memory to be read or written just like numbers (data). Simplifies SW/HW of computer systems: Memory technology for data also used for programs 50 25

26 Result #1: Everything Addressed Since all instructions and data are stored in memory as numbers, everything has a memory address: instructions, data words both branches and jumps use these C pointers are just memory addresses: they can point to anything in memory Unconstrained use of addresses can lead to nasty bugs; up to you in C; limits in Java One register keeps address of instruction being executed: Program Counter (PC) Basically a pointer to memory: Intel calls it Instruction Address Pointer, which is better 51 Result #2: Binary Compatibility Programs are distributed in binary form Programs bound to specific instruction set Different version for Macintosh and IBM PC New machines want to run old programs ( binaries ) as well as programs compiled to new instructions Leads to instruction set evolving over time Selection of Intel 8086 in 1981 for 1st IBM PC is major reason latest PCs still use 80x86 instruction set (Pentium 4); could still run program from 1981 PC today 52 26

27 Table of Content Introduction MIPS architecture CPU registers Memory map Basis of MIPS assembly language Use of labels Addressing Input/Output Instructions MIPS programming skills Control structures Stack and procedure calls How to write assembly language programs Summary 53 I/O How a MIPS program to communicate with the user? I/O provides means for writing to the console and reading from console. Example: suppose we d like to print $t0 content to the console as an integer value, following 3 steps: Place $t0 content in $a0 (e.g., add $a0, $t0, $0) Place a code print integer (1) in $v0 (e.g., addi $v0, $0, 1) Execute the instruction syscall Code:.text addi $v0, $0, 1 #place the value 1 in $v0 add $a0, $t0, $0 #place $t0 content in $a0 syscall MIPS defines other I/O functions, e.g., print character (4), read from console (5). We now study MIPS ASM I/O by examples 54 27

28 I/O Example (1) How to print string Enter Number: to the console? Note the code for print string is 4.data str:.asciiz Enter Number:.text addi $v0, $0, 4 #code for print string la $a0, str syscall #$a0 = string address # will cause the string to # be printed in the console Here, a new instruction la (load address) is used to place the value of str into a register. $a0 content is the starting address of the string (str). The string Enter Number: is terminated in memory by a null termination character. 55 I/O Example (2) How to get the value typed in the console window (by the user) into a register? There is a I/O syscall with code 5 for doing this. The value will be stored in the register $v0, and can be later moved to other space..data str:.asciiz Enter Number: L1:.word 0x0 # has the same effect as.space 4.text addi $v0, $0, 4 #code for print string la $a0, str #$a0 = string address syscall # print string in the console addi $v0, $0, 5 #code for reading from console syscall # read the value typed in the console sw $v0, L1($0) # store the value into the data segment 56 28

29 Table of Content Introduction MIPS architecture CPU registers Memory map Basis of MIPS assembly language Use of labels Addressing Input/Output Instructions MIPS programming skills Control structures Stack and procedure calls How to write assembly language programs Summary 57 Instructions: a Review (registers): C variables: $s0-$s7 Temporary variables: $t0-$t9 Zero: $zero Register content is viewed as single 32-bit number (such as signed or unsigned integer): We have learnt some memory access instructions: lw, sw Arithmetic: add, addi, sub Branches and jumps View register content in bit-wise way: Logical instructions shifts 58 29

30 DataPath Diagram Program Counter (PC) Cache Memory Out Instruction Register ALU Address Control Logic Rs Rd Rt Data In Register File 4 59 Addition and Subtraction Syntax of Instructions: 1 2, 3, 4 where: 1) operation by name 2) operand getting result (destination) 3) 1st operand for operation (source1) 4) 2nd operand for operation (source2) Syntax is rigid: 1 operator, 3 operands Why? Keep Hardware simple via regularity 60 30

31 Instructions as Numbers Currently all data we work with is in words (32-bit blocks): Each register is a word. lw and sw both access memory one word at a time. So how do we represent instructions? Remember: Computer only understands 1s and 0s, so add $t0,$0,$0 is meaningless. MIPS wants simplicity: since data is in words, make instructions be words... One word is 32 bits, so divide instruction word into fields. Each field tells computer something about instruction. We could define different fields for each instruction, but MIPS is based on simplicity, so define 3 basic types of instruction formats: R-format I-format J-format 61 Three Instruction Word Formats Register Format Op-Code Rs Rt Rd Code Op-Code Rs Rt Rd Code Immediate Format Op-Code Rs Rt 16 - Bit Immediate Value Op-Code Rs Rt 16 - Bit Immediate Value Jump Format Op-Code Bit Bit Current Current Segment Address Address

32 A Register Transfer Description of the Control Logic IR = Mem[PC] PC = PC + 4 Decode Instruction lw or sw Read from Reg. File Address = Rs + Offset R-Type sw Memory[Address] = Rt lw beqz If (Rs == 0 ) then PC = PC + Offset Reg. File[Rt] = Memory[Address] Reg. File[Rd] = Rs operation Rt 63 Branches and jumps Branch instructions (beq, bgez, bne, beqz, ble, bnez, etc.) use a signed 16-bit offset field; hence they can jump 2^15-1 instructions (not bytes) forward or 2^15 instructions backward. How do we usually use branches? Answer: if-else, while, for Function calls and unconditional jumps are done using jump instructions (j and jal), not the branches. The jump instruction contains a 26-bit address field

33 Branch Example (1/2) MIPS Code: Loop: beq $9,$0,End add $8,$8,$10 addi $9,$9,-1 j Loop End: Immediate Field in page 62: Number of instructions to add to (or subtract from) the PC, starting at the instruction following the branch. In this case, immediate = 3 65 Branch Example (2/2) MIPS Code: Loop: beq $9,$0,End add $8,$8,$10 End: addi $9,$9,-1 j Loop decimal representation: binary representation:

34 Translation of if then -- else if ($t8 < 0) then {$s0 = 0 - $t8; $t1 = $t1 +1} else {$s0 = $t8; $t2 = $t2 + 1} Translation of pseudocode to MIPS assembly language. Comments are very useful - notice how the comments in the code below help to make the connection back to the original pseudocode. bgez $t8, else # if ($t8 is > or = zero) branch to else sub $s0, $zero, $t8 # $s0 gets the negative of $t8 addi $t1, $t1, 1 # increment $t1 by 1 b next # branch around the else code else: ori $s0, $t8, 0 # $s0 gets a copy of $t8 addi $t2, $t2, 1 # increment $t2 by 1 next: 67 Translation of a While statement $v0 = 1 While ($a1 < $a2) do {$t1 = mem[$a1]; $t2 = mem[$a2]; If ($t1!= $t2) go to break; $a1 = $a1 +1; $a2 = $a2 1;} return break: $v0 = 0 return Here is a translation of the above while pseudocode into MIPS assembly language code. li $v0, 1 # Load $v0 with the value 1 loop: bgeu $a1, $a2, done # If( $a1 >= $a2) Branch to done lb $t1, 0($a1) # Load a Byte: $t1 = mem[$a1 + 0] lb $t2, 0($a2) # Load a Byte: $t2 = mem[$a2 + 0] bne $t1, $t2, break # If ($t1!= $t2) Branch to break addi $a1, $a1, 1 # $a1 = $a1 + 1 addi $a2, $a2, -1 # $a2 = $a2-1 b loop # Branch to loop break: li $v0, 0 # Load $v0 with the value 0 done: 68 34

35 Translation of a for loop $a0 = 0; For ( $t0 =10; $t0 > 0; $t0 = $t0-1) do {$a0 = $a0 + $t0} The following is a translation of the above for-loop pseudocode to MIPS assembly language code. li $a0, 0 # $a0 = 0 li $t0, 10 # Initialize loop counter to 10 loop: add $a0, $a0, $t0 addi $t0, $t0, -1 # Decrement loop counter bgtz $t0, loop # If ($t0 >0) Branch to loop 69 Things to Remember Simplifying MIPS: Define instructions to be same size as data (one word) so that they can use the same memory (can use lw and sw). Machine Language Instruction: 32 bits representing a single instruction R I opcode rs rt rd shamt funct opcode rs rt immediate Computer actually stores programs as a series of these

36 I-Format Problem (1/2) It is possible addi, lw, and sw will use immediates small enough to fit in the immediate field. New instruction: lui register, immediate stands for Load Upper Immediate takes 16-bit immediate and puts these bits in the upper half (high order half) of the specified register sets lower half to 0s 71 I-Format Problem (2/2) Solution to the Problem (continued): So how does lui help us? Example: addi $t0,$t0, 0xABABCDCD becomes: lui $at, 0xABAB ori $at, $at, 0xCDCD add $t0,$t0,$at Now each I-format instruction has only a 16-bit immediate

37 Some Logical Instructions NOT rdest, rsrc OR rd, rs, rt ORI rt, rs, imm AND rd, rs, rt ANDI rt, rs, imm and so on 73 Table of Content Introduction MIPS architecture CPU registers Memory map Basis of MIPS assembly language Use of labels Addressing Input/Output Instructions MIPS programming skills Control structures Stack and procedure calls How to write assembly language programs Summary 74 37

38 Structured Programming MIPS assembly language allows us build programs for MIPS processors, based on a the MIPS instruction set To build a reliable program, structured programming is useful. Programs are build up of certain blocks, each with a single entry point and a single exit point. No jump into a block, or bugs may happen Q: How is a code block implemented in assembly language? 75 Three control structures of programming Code blocks in sequence Instructions are executed in sequence by default Alternation (If-then-else) of two code blocks In implementation, this may involve branch and other instructions Q: is if-endif also structured programming? Iteration (while-loop) of a code block Q: Would it be a good idea to implement a complicated code block as a subroutine (as a procedure call / function)? 76 38

39 Building Structures in Assembly Language Way: by using two types of instructions Jump instructions j instruction (jump) Conditional branch instructions beq instruction (branch equal) bne instruction (branch not equal) 77 Jump instruction jump corresponds to a change in PC (which needs a branch delay to be finished) J-format instruction can only specify 26-bit address j target # after a delay of one machine cycle # PC addr of target This 26bit address is transformed into a 32bit address as follows: 6bit 26bit Machine code: PC: bit jump-addr: Q: Before jumping to the 32bit jump-address, which address is in the PC? 78 39

40 Conditional Branches A conditional branch instruction branches to a new address only if a certain condition is true. beq u,v,addr # if register $u == register $v # PC addr (after a branch delay) #else # no effect. The same restriction as jump instruction is that they should also be limited to 26bit addr ( local ) 79 Ifs and whiles Branch instructions are used to implement both loops and branches. An example based on beq:... # load values into $8 and $9 beq $8,$9,cont # branch if equal nop # branch delay slot... # conditionally... # executed... # instructions cont: add $10,$10,$11 # always executed CONT beq $8, $9, CONT Conditionally executed instructions $8 == $

41 Alternations A conditional branch (e.g., beq) + a jump... # load values into # $8 and $9 beq $8,$9,equal # branch if equal nop # branch delay slot... #... # false branch... # j cont nop equal:... #... # true branch... # cont: add $10,$10,$11 # always executed $8 $9 $8 == $9 beq $8, $9, CONT False Branch CONT True Branch 81 Loop and branch example: string length str:.data.asciiz "Time is space." This represents a string Time\0x20is\0x20the\0x20ghost\0x20of\0x20space.\0x00 How this is stored in MIPS memory? Data segment e m i T _ si _ c aps /.e [0x ] 0x656d6954 0x x x00002e65 Q: Should \0x00 (null) also be counted as 1 byte of the string? 82 41

42 .data string:.asciiz "Time is space." ## End of file String length (cont.) ## start ## Count the characters in a string ## ## Registers: count = 0 ## $8 --- count ## $9 --- pointer to the char ## $ the char (in low order byte) point to first char ##.text.globl main # Initialize char is null? YES main: ori $8,$0,0 # count = 0 lui $9, 0x1001 # point to first char # while not ch==null do loop: lbu $10, ($9) # get the char count ++ $10, $0, done # exit loop if char == null addiu,, # count ++ addiu,, # point at next char point to next char j loop # finish done: sll $0,$0,0 # shift left logically, cause a branch delay end 83 How viruses work Virus usually written in assembly language Inserted into another program Virus dormant until program executed Then infects other programs Eventually executes its payload Types: companion, executable program, memory, boot sector, macro, source code 84 42

43 Starting address Virus example: executable program Executable program Header a) b) c) d) A non-infected executable program in the file system With a parasitic virus at the front at the end Spread over free space within the program (cavity virus) When run the infected file, the virus is loaded into memory and may infect others Q: Which instruction in ASM will be the first instruction on start of a text segment? Executable program Virus Header Virus Executable program Header Virus Virus Virus Virus Header 85 Table of Content Introduction MIPS architecture CPU registers Memory map Basis of MIPS assembly language Use of labels Addressing Input/Output Instructions MIPS programming skills Control structures Stack and procedure calls How to write assembly language programs Summary 86 43

44 Stack Stack: LIFO (Last In First Out) data structure Stack pointer register in MIPS Point to the top of the stack $sp, namely $29 Push: Pop: 0x7FFFFFFC 0x7FFFFFF9 0x7FFFFFF5 # PUSH the item in $t0: sub $sp,$sp,4 # point to the place for the new item, sw $t0,($sp) # store $t0 content as the new top. # POP the item into $t0: lw $t0,($sp) # Copy top the item to $t0. add $sp,$sp,4 # Point to the item beneath the old top. Q: When an item in the stack is popped, does the item value remain in memory? $sp $sp Run-time Stack Data and stack segments shares a section of memory space With a program runs: the stack grows downward into the available space the data segment grows upward Initial value of $sp is 0x7FFFFFFC 0xFFFF FFFF. 0x x7FFFFFFF 0x x x Reserved for ROM and OS Stack Segment Data Segment Text Segment Reserved MIPS memory map 88 44

45 Subroutine All high level languages have the concept of a subroutine (sometimes called procedure, function, or method). A subroutine is a logical division of the code that may be regarded as a self-contained operation. A subroutine might be executed several times with different data as the program executes. In assembly languages, there is also such concept. 89 Callers and Callees A subroutine call is when a main routine (or other routine) passes control to a subroutine. The main routine is said to be the CALLER and the subroutine is said to be the CALLEE. A return from a subroutine is when a subroutine passes control back to its CALLER

46 Callers and Callees (cont.) When the CALLEE finishes execution it normally returns control to its CALLER, i.e., load the PC (program counter) with the return address. The next instruction fetch of the machine cycle will get the instruction from that address. Q: How should the return address be passed to the subroutine? (i) By placing it in main memory somewhere, or (ii) By placing it in a register designated for this purpose. 91 jal instruction The register that is used for linkage is register $31, namely $ra. It holds the return address for a subroutine. jal instruction puts the return address into $ra. How the jal instruction works? # when the jal is loaded the PC has 0x # next (in the machine cycle) the PC is increased to 0x jal sub: $ra PC+4 # $ra 0x (address 8 bytes away from the jal) PC sub # load the PC with 0x (the subroutine entry point) 92 46

47 jr instruction The jr instruction is used to return to the callee. It copies the contents of e.g., $ra into the PC: jr $ra # PC $ra 93 Simple Subroutine Conventions A subroutine is called using jal. A subroutine will NOT call another subroutine. The subroutine returns to its caller using jr $ra. Register use is as follows: $t0 - $t9 The subroutine is free to change these registers. $s0 - $s7 The subroutine must not change these registers. $a0 - $a3 These registers contain arguments for the subroutine. The subroutine can change them. $v0 - $v1 These registers contain values returned from the subroutine. $ra return address $sp stack pointer $gp global pointer, used to access static variables 94 47

48 What happens while executing a subroutine Before entering: Pass arguments into registers $a1-$a3 from left to right, and pushing any other arguments into the stack from right to left. Save registers used by the caller (PUSH) Execute a jal instruction to jump to a callee s first instruction and save the return address in $ra. When returning: Place in the return values in registers $v0-$v1 Restore all registers saved while entering (POP) End subroutine with a jump register instruction (jr $ra) 95 Example Goal: to read three integers from the user and compute the sum Outline of the program: # read first integer # read second integer # read third integer # compute the sum # write out the result 96 48

49 Subroutine pread # pread -- prompt for and read an integer # # on entry: # $ra -- return address # # on exit: # $v0 -- the integer.text.globl pread pread: la $a0,prompt # print string li $v0,4 # service 4 syscall li $v0,5 # read int into $v0 syscall # service 5 # return.data prompt:.asciiz "Enter an integer:" 97 Main program.text.globl main main: jal # read first integer move $s0,$v0 # save it in $s0 jal # read second integer move $s1,$v0 # save it in $s1 jal # read third integer move $s2,$v0 # save it in $s2 addu $s0,$s0,$s1 # compute the sum addu $a0,$s0,$s2 li $v0,1 # print the sum syscall li $v0,10 # exit syscall 98 49

50 Table of Content Introduction MIPS architecture CPU registers Memory map Basis of MIPS assembly language Use of labels Addressing Input/Output Instructions MIPS programming skills Control structures Procedure call How to write assembly language programs Summary 99 Programming in assembly language Task analysis, divide it into modules (blocks) Draw a flow chart for each module. Create pseudo-codes as comments without executable instructions. Distinguish between constants and variables needed Constant strings ASCII text in the program.data area Variables registers (or RAM when large amount) & allocation Identify the right places of labels for Entry points to module Return points for loops Destination of forward branch Work through the flow chart to implement the assembly instructions Test by assembling the program and running it Testing can be done on the actual target machine or using a simulator. In our case we use SPIM simulator

51 Summary Assembly language v.s. High-level languages Assembly language is machine-specific MIPS ASM is for MIPS machines, a RISC architecture MIPS registers: PC, $sp, $ra, other general registers Memory map and addressing, ref. CPU datapath cycle Basic arithmetic & memory operations, data segment & alignment Concept of pseudo-instructions Input/Output Branch and jump instructions control structures Stack and procedure calls; register usage conventions MIPS assembly language programming by examples; run & test in SPIM simulator 101 Instruction Categories Load/Store Computational (arithmetic) Jump and Branch Floating Point (coprocessor) Memory Management Special: I/O MIPS Architecture (Summary) Registers R0 - R31 PC HI LO 3 Instruction Formats: all 32 bits wide R: I: J: OP Rs Rt rd sa funct OP Rs Rt Immediate OP jump target MIPS memory map 0xFFFF FFFF. 0x x7FFFFFFF 0x x x Reserved for ROM and OS Stack Segment Data Segment Text Segment Reserved

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson CS 430 Computer Architecture C/Assembler Arithmetic and Memory Access William J. Taffe using notes of David Patterson 1 Overview C operators, operands Variables in Assembly: Registers Comments in Assembly

More information

Chapter 2A Instructions: Language of the Computer

Chapter 2A Instructions: Language of the Computer Chapter 2A Instructions: Language of the Computer Copyright 2009 Elsevier, Inc. All rights reserved. Instruction Set The repertoire of instructions of a computer Different computers have different instruction

More information

CS 61c: Great Ideas in Computer Architecture

CS 61c: Great Ideas in Computer Architecture MIPS Instruction Formats July 2, 2014 Review New registers: $a0-$a3, $v0-$v1, $ra, $sp New instructions: slt, la, li, jal, jr Saved registers: $s0-$s7, $sp, $ra Volatile registers: $t0-$t9, $v0-$v1, $a0-$a3

More information

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson.

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson. CS61C - Machine Structures Lecture 6 - Instruction Representation September 15, 2000 David Patterson http://www-inst.eecs.berkeley.edu/~cs61c/ 1 Review Instructions: add, addi, sub, lw, sw beq, bne, j

More information

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine Machine Language Instructions Introduction Instructions Words of a language understood by machine Instruction set Vocabulary of the machine Current goal: to relate a high level language to instruction

More information

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei Instruction Set Architecture part 1 (Introduction) Mehran Rezaei Overview Last Lecture s Review Execution Cycle Levels of Computer Languages Stored Program Computer/Instruction Execution Cycle SPIM, a

More information

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions Representing Instructions Instructions are encoded in binary Called machine code MIPS instructions Encoded as 32-bit instruction words Small number of formats encoding operation code (opcode), register

More information

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture EEM 486: Computer Architecture Lecture 2 MIPS Instruction Set Architecture EEM 486 Overview Instruction Representation Big idea: stored program consequences of stored program Instructions as numbers Instruction

More information

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats Instructor: Justin Hsia 6/27/2012 Summer 2012 Lecture #7 1 Review of Last Lecture New registers: $a0-$a3, $v0-$v1, $ra, $sp Also: $at,

More information

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University. Instructions: ti Language of the Computer Rui Wang, Assistant professor Dept. of Information and Communication Tongji University it Email: ruiwang@tongji.edu.cn Computer Hierarchy Levels Language understood

More information

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009 101 Assembly ENGR 3410 Computer Architecture Mark L. Chang Fall 2009 What is assembly? 79 Why are we learning assembly now? 80 Assembly Language Readings: Chapter 2 (2.1-2.6, 2.8, 2.9, 2.13, 2.15), Appendix

More information

CENG3420 Lecture 03 Review

CENG3420 Lecture 03 Review CENG3420 Lecture 03 Review Bei Yu byu@cse.cuhk.edu.hk 2017 Spring 1 / 38 CISC vs. RISC Complex Instruction Set Computer (CISC) Lots of instructions of variable size, very memory optimal, typically less

More information

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization CISC 662 Graduate Computer Architecture Lecture 4 - ISA MIPS ISA Michela Taufer http://www.cis.udel.edu/~taufer/courses Powerpoint Lecture Notes from John Hennessy and David Patterson s: Computer Architecture,

More information

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University Introduction to the MIPS Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University Introduction to the MIPS The Microprocessor without Interlocked Pipeline Stages

More information

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2) ELEC 5200-001/6200-001 Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2) Victor P. Nelson, Professor & Asst. Chair Vishwani D. Agrawal, James J. Danaher Professor Department

More information

MIPS (SPIM) Assembler Syntax

MIPS (SPIM) Assembler Syntax MIPS (SPIM) Assembler Syntax Comments begin with # Everything from # to the end of the line is ignored Identifiers are a sequence of alphanumeric characters, underbars (_), and dots () that do not begin

More information

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands Stored Program Concept Instructions: Instructions are bits Programs are stored in memory to be read or written just like data Processor Memory memory for data, programs, compilers, editors, etc. Fetch

More information

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes Chapter 2 Instructions: Language of the Computer Adapted by Paulo Lopes Instruction Set The repertoire of instructions of a computer Different computers have different instruction sets But with many aspects

More information

COMPUTER ORGANIZATION AND DESIGN

COMPUTER ORGANIZATION AND DESIGN COMPUTER ORGANIZATION AND DESIGN 5 th The Hardware/Software Interface Edition Chapter 2 Instructions: Language of the Computer 2.1 Introduction Instruction Set The repertoire of instructions of a computer

More information

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA CISC 662 Graduate Computer Architecture Lecture 4 - ISA Michela Taufer http://www.cis.udel.edu/~taufer/courses Powerpoint Lecture Notes from John Hennessy and David Patterson s: Computer Architecture,

More information

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA. Today s topics CS/COE1541: Introduction to Computer Architecture MIPS operations and operands MIPS registers Memory view Instruction encoding A Review of MIPS ISA Sangyeun Cho Arithmetic operations Logic

More information

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook) Lecture 2 Instructions: Language of the Computer (Chapter 2 of the textbook) Instructions: tell computers what to do Chapter 2 Instructions: Language of the Computer 2 Introduction Chapter 2.1 Chapter

More information

Computer Architecture

Computer Architecture Computer Architecture Chapter 2 Instructions: Language of the Computer Fall 2005 Department of Computer Science Kent State University Assembly Language Encodes machine instructions using symbols and numbers

More information

Lecture #6 Intro MIPS; Load & Store

Lecture #6 Intro MIPS; Load & Store CS61C L6 Intro MIPS ; Load & Store (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #6 Intro MIPS; Load & Store 2007-7-3 Scott Beamer, Instructor Interesting Research on Social Sites

More information

Computer Organization MIPS ISA

Computer Organization MIPS ISA CPE 335 Computer Organization MIPS ISA Dr. Iyad Jafar Adapted from Dr. Gheith Abandah Slides http://www.abandah.com/gheith/courses/cpe335_s08/index.html CPE 232 MIPS ISA 1 (vonneumann) Processor Organization

More information

We will study the MIPS assembly language as an exemplar of the concept.

We will study the MIPS assembly language as an exemplar of the concept. MIPS Assembly Language 1 We will study the MIPS assembly language as an exemplar of the concept. MIPS assembly instructions each consist of a single token specifying the command to be carried out, and

More information

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions Orange Coast College Business Division Computer Science Department CS 116- Computer Architecture The Instructions 1 1 Topics: Assembly language, assemblers MIPS R2000 Assembly language Instruction set

More information

Reduced Instruction Set Computer (RISC)

Reduced Instruction Set Computer (RISC) Reduced Instruction Set Computer (RISC) Reduced Instruction Set Computer (RISC) Focuses on reducing the number and complexity of instructions of the machine. Reduced number of cycles needed per instruction.

More information

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#: Computer Science and Engineering 331 Midterm Examination #1 Fall 2000 Name: Solutions S.S.#: 1 41 2 13 3 18 4 28 Total 100 Instructions: This exam contains 4 questions. It is closed book and notes. Calculators

More information

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Computer Organization and Structure. Bing-Yu Chen National Taiwan University Computer Organization and Structure Bing-Yu Chen National Taiwan University Instructions: Language of the Computer Operations and Operands of the Computer Hardware Signed and Unsigned Numbers Representing

More information

MIPS Assembly Language Programming

MIPS Assembly Language Programming MIPS Assembly Language Programming COE 308 Computer Architecture Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals Presentation Outline Assembly

More information

Topic Notes: MIPS Instruction Set Architecture

Topic Notes: MIPS Instruction Set Architecture Computer Science 220 Assembly Language & Comp. Architecture Siena College Fall 2011 Topic Notes: MIPS Instruction Set Architecture vonneumann Architecture Modern computers use the vonneumann architecture.

More information

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary Control Instructions Computer Organization Architectures for Embedded Computing Thursday, 26 September 2013 Many slides adapted from: Computer Organization and Design, Patterson & Hennessy 4th Edition,

More information

Control Instructions

Control Instructions Control Instructions Tuesday 22 September 15 Many slides adapted from: and Design, Patterson & Hennessy 5th Edition, 2014, MK and from Prof. Mary Jane Irwin, PSU Summary Previous Class Instruction Set

More information

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont ) Chapter 2 Computer Abstractions and Technology Lesson 4: MIPS (cont ) Logical Operations Instructions for bitwise manipulation Operation C Java MIPS Shift left >>> srl Bitwise

More information

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Computer Organization and Structure. Bing-Yu Chen National Taiwan University Computer Organization and Structure Bing-Yu Chen National Taiwan University Instructions: Language of the Computer Operations and Operands of the Computer Hardware Signed and Unsigned Numbers Representing

More information

Reduced Instruction Set Computer (RISC)

Reduced Instruction Set Computer (RISC) Reduced Instruction Set Computer (RISC) Focuses on reducing the number and complexity of instructions of the ISA. RISC Goals RISC: Simplify ISA Simplify CPU Design Better CPU Performance Motivated by simplifying

More information

CS3350B Computer Architecture MIPS Introduction

CS3350B Computer Architecture MIPS Introduction CS3350B Computer Architecture MIPS Introduction Marc Moreno Maza http://www.csd.uwo.ca/~moreno/cs3350_moreno/index.html Department of Computer Science University of Western Ontario, Canada Thursday January

More information

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015 Branch Addressing Branch instructions specify Opcode, two registers, target address Most branch targets are near branch Forward or backward op rs rt constant or address 6 bits 5 bits 5 bits 16 bits PC-relative

More information

Instructions: Assembly Language

Instructions: Assembly Language Chapter 2 Instructions: Assembly Language Reading: The corresponding chapter in the 2nd edition is Chapter 3, in the 3rd edition it is Chapter 2 and Appendix A and in the 4th edition it is Chapter 2 and

More information

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization CSE2021 Computer Organization Chapter 2: Part 2 Procedure Calling Procedure (function) performs a specific task and return results to caller. Supporting Procedures Procedure Calling Calling program place

More information

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011 CSE 2021: Computer Organization Recap from Last Time load from disk High-Level Program Lecture-3 Code Translation-1 Registers, Arithmetic, logical, jump, and branch instructions MIPS to machine language

More information

Lecture 5: Procedure Calls

Lecture 5: Procedure Calls Lecture 5: Procedure Calls Today s topics: Memory layout, numbers, control instructions Procedure calls 1 Memory Organization The space allocated on stack by a procedure is termed the activation record

More information

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers CSE 2021: Computer Organization Recap from Last Time load from disk High-Level Program Lecture-2 Code Translation-1 Registers, Arithmetic, logical, jump, and branch instructions MIPS to machine language

More information

MIPS Assembly Language Programming

MIPS Assembly Language Programming MIPS Assembly Language Programming ICS 233 Computer Architecture and Assembly Language Dr. Aiman El-Maleh College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals [Adapted

More information

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions Procedure Calls A procedure of a subroutine is like an agent which needs certain information to perform a

More information

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2) Introduction to the MIPS ISA Overview Remember that the machine only understands very basic instructions (machine instructions) It is the compiler s job to translate your high-level (e.g. C program) into

More information

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from: Instructions: Chapter 3 : MIPS Downloaded from: http://www.cs.umr.edu/~bsiever/cs234/ Language of the Machine More primitive than higher level languages e.g., no sophisticated control flow Very restrictive

More information

Computer Architecture. The Language of the Machine

Computer Architecture. The Language of the Machine Computer Architecture The Language of the Machine Instruction Sets Basic ISA Classes, Addressing, Format Administrative Matters Operations, Branching, Calling conventions Break Organization All computers

More information

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture Computer Science 324 Computer Architecture Mount Holyoke College Fall 2009 Topic Notes: MIPS Instruction Set Architecture vonneumann Architecture Modern computers use the vonneumann architecture. Idea:

More information

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations

Review. Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I Logical Operators (1/3) Bitwise Operations CS6C L9 MIPS Logical & Shift Ops, and Instruction Representation I () inst.eecs.berkeley.edu/~cs6c CS6C : Machine Structures Lecture #9 MIPS Logical & Shift Ops, and Instruction Representation I 25-9-28

More information

Computer Architecture

Computer Architecture CS3350B Computer Architecture Winter 2015 Lecture 4.2: MIPS ISA -- Instruction Representation Marc Moreno Maza www.csd.uwo.ca/courses/cs3350b [Adapted from lectures on Computer Organization and Design,

More information

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College Assembly Language Programming CPSC 252 Computer Organization Ellen Walker, Hiram College Instruction Set Design Complex and powerful enough to enable any computation Simplicity of equipment MIPS Microprocessor

More information

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits

I-Format Instructions (3/4) Define fields of the following number of bits each: = 32 bits CS61C L10 MIPS Instruction Representation II (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #10 Instruction Representation II 2007-7-8 Review There are register calling conventions!

More information

CS3350B Computer Architecture

CS3350B Computer Architecture CS3350B Computer Architecture Winter 2015 Lecture 4.1: MIPS ISA: Introduction Marc Moreno Maza www.csd.uwo.ca/courses/cs3350b [Adapted d from lectures on Computer Organization and Design, Patterson & Hennessy,

More information

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1 Chapter 3 MIPS Assembly Language Ó1998 Morgan Kaufmann Publishers 1 Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control flow Very restrictive

More information

MIPS Assembly Programming

MIPS Assembly Programming COMP 212 Computer Organization & Architecture COMP 212 Fall 2008 Lecture 8 Cache & Disk System Review MIPS Assembly Programming Comp 212 Computer Org & Arch 1 Z. Li, 2008 Comp 212 Computer Org & Arch 2

More information

MIPS Instruction Set

MIPS Instruction Set MIPS Instruction Set Prof. James L. Frankel Harvard University Version of 7:12 PM 3-Apr-2018 Copyright 2018, 2017, 2016, 201 James L. Frankel. All rights reserved. CPU Overview CPU is an acronym for Central

More information

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei Computer Architecture Instruction Set Architecture part 2 Mehran Rezaei Review Execution Cycle Levels of Computer Languages Stored Program Computer/Instruction Execution Cycle SPIM, a MIPS Interpreter

More information

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Architecture II. Computer Systems Laboratory Sungkyunkwan University MIPS Instruction ti Set Architecture II Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Making Decisions (1) Conditional operations Branch to a

More information

CS3350B Computer Architecture MIPS Instruction Representation

CS3350B Computer Architecture MIPS Instruction Representation CS3350B Computer Architecture MIPS Instruction Representation Marc Moreno Maza http://www.csd.uwo.ca/~moreno/cs3350_moreno/index.html Department of Computer Science University of Western Ontario, Canada

More information

MIPS Instruction Set Architecture (2)

MIPS Instruction Set Architecture (2) MIPS Instruction Set Architecture (2) Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3050: Theory on Computer Architectures, Spring 2017, Jinkyu

More information

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE 1 ARCHITECTURE MODEL The basic instruction set of a computer is comprised of sequences of REGISTER TRANSFERS. Example: Add A, B, C Register B # A

More information

Computer Architecture. MIPS Instruction Set Architecture

Computer Architecture. MIPS Instruction Set Architecture Computer Architecture MIPS Instruction Set Architecture Instruction Set Architecture An Abstract Data Type Objects Registers & Memory Operations Instructions Goal of Instruction Set Architecture Design

More information

MIPS Functions and Instruction Formats

MIPS Functions and Instruction Formats MIPS Functions and Instruction Formats 1 The Contract: The MIPS Calling Convention You write functions, your compiler writes functions, other compilers write functions And all your functions call other

More information

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design Professor Sherief Reda http://scale.engin.brown.edu School of Engineering Brown University Spring 2014 Sources: Computer

More information

CS 110 Computer Architecture MIPS Instruction Formats

CS 110 Computer Architecture MIPS Instruction Formats CS 110 Computer Architecture MIPS Instruction Formats Instructor: Sören Schwertfeger http://shtech.org/courses/ca/ School of Information Science and Technology SIST ShanghaiTech University Slides based

More information

ECE 154A Introduction to. Fall 2012

ECE 154A Introduction to. Fall 2012 ECE 154A Introduction to Computer Architecture Fall 2012 Dmitri Strukov Lecture 4: Arithmetic and Data Transfer Instructions Agenda Review of last lecture Logic and shift instructions Load/store instructionsi

More information

MIPS%Assembly% E155%

MIPS%Assembly% E155% MIPS%Assembly% E155% Outline MIPS Architecture ISA Instruction types Machine codes Procedure call Stack 2 The MIPS Register Set Name Register Number Usage $0 0 the constant value 0 $at 1 assembler temporary

More information

Lecture 4: MIPS Instruction Set

Lecture 4: MIPS Instruction Set Lecture 4: MIPS Instruction Set No class on Tuesday Today s topic: MIPS instructions Code examples 1 Instruction Set Understanding the language of the hardware is key to understanding the hardware/software

More information

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands Stored Program Concept nstructions: nstructions are bits Programs are stored in memory to be read or written just like data Processor Memory memory for data, programs, compilers, editors, etc. Fetch &

More information

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro 1 Levels of Representation/Interpretation Machine Interpretation High Level Language Program (e.g., C) Compiler Assembly

More information

מבנה מחשבים Amar Lior Based on lectures notes from Arie Schlesinger

מבנה מחשבים Amar Lior Based on lectures notes from Arie Schlesinger מבנה מחשבים 2006 Amar Lior Based on lectures notes from Arie Schlesinger (aries@cs.columbia.edu) Adapted from Computer Organization&Design, H/S interface, Patterson Hennessy@UCB,1999 1 Administration Course

More information

Thomas Polzer Institut für Technische Informatik

Thomas Polzer Institut für Technische Informatik Thomas Polzer tpolzer@ecs.tuwien.ac.at Institut für Technische Informatik Branch to a labeled instruction if a condition is true Otherwise, continue sequentially beq rs, rt, L1 if (rs == rt) branch to

More information

All instructions have 3 operands Operand order is fixed (destination first)

All instructions have 3 operands Operand order is fixed (destination first) Instruction Set Architecture for MIPS Processors Overview Dr. Arjan Durresi Louisiana State University Baton Rouge, LA 70803 durresi@csc.lsu.edu These slides are available at: http://www.csc.lsu.edu/~durresi/_07/

More information

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25 Comparisons Set on less than (slt) compares its source registers

More information

Instruction Set Architecture. "Speaking with the computer"

Instruction Set Architecture. Speaking with the computer Instruction Set Architecture "Speaking with the computer" The Instruction Set Architecture Application Compiler Instr. Set Proc. Operating System I/O system Instruction Set Architecture Digital Design

More information

EEC 581 Computer Architecture Lecture 1 Review MIPS

EEC 581 Computer Architecture Lecture 1 Review MIPS EEC 581 Computer Architecture Lecture 1 Review MIPS 1 Supercomputing: Suddenly Fancy 2 1 Instructions: Language of the Machine More primitive than higher level languages e.g., no sophisticated control

More information

CS 61c: Great Ideas in Computer Architecture

CS 61c: Great Ideas in Computer Architecture MIPS Functions July 1, 2014 Review I RISC Design Principles Smaller is faster: 32 registers, fewer instructions Keep it simple: rigid syntax, fixed instruction length MIPS Registers: $s0-$s7,$t0-$t9, $0

More information

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018

ECE 331 Hardware Organization and Design. Professor Jay Taneja UMass ECE - Discussion 3 2/8/2018 ECE 331 Hardware Organization and Design Professor Jay Taneja UMass ECE - jtaneja@umass.edu Discussion 3 2/8/2018 Study Jams Leader: Chris Bartoli Tuesday 5:30-6:45pm Elab 325 Wednesday 8:30-9:45pm Elab

More information

Chapter 2. Instructions: Language of the Computer

Chapter 2. Instructions: Language of the Computer Chapter 2 Instructions: Language of the Computer Instruction Set The repertoire of instructions of a computer Different computers have different instruction sets But with many aspects in common Early computers

More information

EE 361 University of Hawaii Fall

EE 361 University of Hawaii Fall C functions Road Map Computation flow Implementation using MIPS instructions Useful new instructions Addressing modes Stack data structure 1 EE 361 University of Hawaii Implementation of C functions and

More information

CS222: MIPS Instruction Set

CS222: MIPS Instruction Set CS222: MIPS Instruction Set Dr. A. Sahu Dept of Comp. Sc. & Engg. Indian Institute of Technology Guwahati 1 Outline Previous Introduction to MIPS Instruction Set MIPS Arithmetic's Register Vs Memory, Registers

More information

Brought to you by CalLUG (UC Berkeley GNU/Linux User Group). Tuesday, September 20, 6-8 PM in 100 GPB.

Brought to you by CalLUG (UC Berkeley GNU/Linux User Group). Tuesday, September 20, 6-8 PM in 100 GPB. Hate EMACS? Love EMACS? Richard M. Stallman, a famous proponent of opensource software, the founder of the GNU Project, and the author of emacs and gcc, will be giving a speech. We're working on securing

More information

Lecture #6 Intro MIPS; Load & Store Assembly Variables: Registers (1/4) Review. Unlike HLL like C or Java, assembly cannot use variables

Lecture #6 Intro MIPS; Load & Store Assembly Variables: Registers (1/4) Review. Unlike HLL like C or Java, assembly cannot use variables CS61C L6 Intro MIPS ; Load & Store (1) Hate EMACS? Love EMACS? Richard M. Stallman, a famous proponent of opensource software, the founder of the GNU Project, and the author of emacs and gcc, will be giving

More information

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set COMPSCI 313 S2 2018 Computer Organization 7 MIPS Instruction Set Agenda & Reading MIPS instruction set MIPS I-format instructions MIPS R-format instructions 2 7.1 MIPS Instruction Set MIPS Instruction

More information

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance Chapter 1 Computer Abstractions and Technology Lesson 3: Understanding Performance Manufacturing ICs 1.7 Real Stuff: The AMD Opteron X4 Yield: proportion of working dies per wafer Chapter 1 Computer Abstractions

More information

LECTURE 2: INSTRUCTIONS

LECTURE 2: INSTRUCTIONS LECTURE 2: INSTRUCTIONS Abridged version of Patterson & Hennessy (2013):Ch.2 Instruction Set The repertoire of instructions of a computer Different computers have different instruction sets But with many

More information

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture Computer Science 324 Computer Architecture Mount Holyoke College Fall 2007 Topic Notes: MIPS Instruction Set Architecture vonneumann Architecture Modern computers use the vonneumann architecture. Idea:

More information

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

ECE232: Hardware Organization and Design. Computer Organization - Previously covered ECE232: Hardware Organization and Design Part 6: MIPS Instructions II http://www.ecs.umass.edu/ece/ece232/ Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Computer Organization

More information

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring 2009 Topic Notes: MIPS Programming We spent some time looking at the MIPS Instruction Set Architecture. We will now consider

More information

Lecture 7: Examples, MARS, Arithmetic

Lecture 7: Examples, MARS, Arithmetic Lecture 7: Examples, MARS, Arithmetic Today s topics: More examples MARS intro Numerical representations 1 Dealing with Characters Instructions are also provided to deal with byte-sized and half-word quantities:

More information

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates

Review of Last Lecture. CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Representation II. Agenda. Dealing With Large Immediates CS 61C: Great Ideas in Computer Architecture MIPS Instruction Representation II Guest Lecturer: Justin Hsia 2/11/2013 Spring 2013 Lecture #9 1 Review of Last Lecture Simplifying MIPS: Define instructions

More information

I ve been getting this a lot lately So, what are you teaching this term? Computer Organization. Do you mean, like keeping your computer in place?

I ve been getting this a lot lately So, what are you teaching this term? Computer Organization. Do you mean, like keeping your computer in place? I ve been getting this a lot lately So, what are you teaching this term? Computer Organization. Do you mean, like keeping your computer in place? here s the monitor, here goes the CPU, Do you need a class

More information

Lectures 3-4: MIPS instructions

Lectures 3-4: MIPS instructions Lectures 3-4: MIPS instructions Motivation Learn how a processor s native language looks like Discover the most important software-hardware interface MIPS Microprocessor without Interlocked Pipeline Stages

More information

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008 Math 230 Assembly Programming (AKA Computer Organization) Spring 2008 MIPS Intro II Lect 10 Feb 15, 2008 Adapted from slides developed for: Mary J. Irwin PSU CSE331 Dave Patterson s UCB CS152 M230 L10.1

More information

CPSC 330 Computer Organization

CPSC 330 Computer Organization CPSC 330 Computer Organization Chapter 2-II Instructions: Language of the computer MIPS Instructions - Review Instruction Meaning add $s1,$s2,$s3 $s1 = $s2 + $s3 sub $s1,$s2,$s3 $s1 = $s2 $s3 addi $s1,$s1,4

More information

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures Review (1/2) CS61C - Machine Structures Lecture 11 - Starting a Program October 4, 2000 David Patterson http://www-inst.eecs.berkeley.edu/~cs61c/ IEEE 754 Floating Point Standard: Kahan pack as much in

More information

MIPS Assembly Language

MIPS Assembly Language MIPS Assembly Language Chapter 15 S. Dandamudi Outline MIPS architecture Registers Addressing modes MIPS instruction set Instruction format Data transfer instructions Arithmetic instructions Logical/shift/rotate/compare

More information

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls Announcements HW1 is due on this Friday (Sept 12 th ) Appendix A is very helpful to HW1. Check out system calls on Page A-48. Ask TA (Liquan chen: liquan@ece.rutgers.edu) about homework related questions.

More information