CH 7 - Assembly Language

8 Pages
143 Views
Unlock Document

Department
Computer Science
Course
CSC258H1
Professor
Steve Engels
Semester
Winter

Description
ASSEMBLY LANGUAGE  MICROPROCESSOR WITHOUT INTERLOCKED PIPELINE STAGES (MIPS)  Intro to Machine Code o Type of REDUCED INSTRUCTION SET COMPUTER (RISC) architecture o Now that we have a processor, operations are performed by: o Provides a set of simple and fast instructions  The instruction register, decoding each instruction according to  Compiler translates instructions into 32-bit instructions for the opcode in the first 6 bits instruction memory  The control unit sending a sequence of signals to the rest of the  Complex instructions (multiplication) are built out of simple processor, according to the opcode value passed in from the ones by the compiler and assembler o Only question remaining: o MIPS is REGISTER-TO-REGISTER  Hwere do these instructions come from?  Every operation operates on data in registers  How are they provided to the instruction memory? o MIPS provides 32 registers, several have secial values  Assembly language  Register 0 ($zero): value 0 o Each processor type has its own language for representing 32-bit  always 0; can’t write into this register, only read instructions as user-level code words  Register 1 ($st): reserved for the assembler o Ex. C = A + B  Store A in $t1, B in $t2, C in $t3  Register 2 – 3 ($v0, $v1): return values  In Assembly Language instruction:  Register 4 – 7 ($a0 – a3): function arguments add $t3, $t1, $t2  Register 8 – 15, 24, 25($t0 – $t9): temporaries  In Machine Code instruction:  Register 16 – 23 ($s0 – $s7): saved temporaries 000000 01001 01010 01011 XXXXX 100000  Register 28 – 31 ($gp, $sp, $fp, $ra): memory and function  Control Unit signals: support  PCWrite = 0 PCWriteCond = 0  $sp is top of the stack, increments to 0  IorD = X  $ra is the return address, the address of a main program  MemRead = 0, MemWrite = 0  when a subroutine complete, $ra is used to return to  MemtoReg = 0 the main program  IRWrite = 0  $fp is the frame pointer  PCSource = X  $gp is the global pointer  ALUOp = 001 (add) o Also three special registers (PC, HI, LO) that are not directly  ALUSrcA = 1, ALUSrcB = 0 accessible  RegWrite = 1, RegDst = 1 (R-type)  HI and LO are used for multiplication and division, and have  Note: most of the time only one write signal is on special instruction for accessing them o Ex. Add 100 to value in PC and store it back into the PC  MIPS Instructions  Operation used by Assembly to jump o Things to note about MIPS instructions  Instruction is addi, the value 100 in instruction[15-0]  Instructions are written as:  ALUSrcB is from Sign Extend, needed b/c the value  Each instruction is written on its own line 100 in instruction[15-0] is 16-bits, need 32-bits  All instructions are 32-bits (4 bytes) long to for ALU to operate on it  Instruction addresses are measured in bytes, starting from the  Control Unit signals: instruction at address 0 o The following tables show the most common MIPS instructions, the  PCWrite = 1 PCWriteCond = x  IorD = x syntax for their parameters, and what operations they perform o Arithemtic instruction  MemRead = 0, MemWrite = 0  MemtoReg = x  add = add 2 registers to a destination register  IRWrite = 0  addi = add an immiedate value (an int) and a register to a destination register  PCSource = 1  ALUOp = 001 (add)  addu = add 2 registers w/ unsigned ints to a destination register Instruction Opcode/ Synatx Operation  ALUSrcA = 0, ALUSrcB = 2  RegWrite = 0, RegDst = x Function add 100000 $d, $s, $t $d = $s + $t o Assembly language instruction is the lowest programmable language addu 100001 $d, $s, $t $d = $s + $t  One-to-one maping with a machine code language addi 001000 $t, $s, i $t = $s + SE(i) o Many compilers translate their high-level program commands into addiu 001001 $t, $s, i $t = $s + SE(i) assembly, which are then converted into machine code and used by div 011010 $s, $t lo = $s / $t; hi = $s % $t the processor divu 011011 $s, $t lo = $s / $t; hi = $s % $t  Note: there are multiple types of assembly language, especially mult 011000 $s, $t hi:lo = $s * $t for different architectures multu 011001 $s, $t hi:lo = $s * $t sub 100010 $d, $s, $t $d = $s - $t subu 100011 $d, $s, $t $d = $s - $t o Logical instructions  bit-wise logical operations Instruction Opcode/ Synatx Operation Function and 100100 $d, $s, $t $d = $s & $t andi 001100 $t, $s, i $t = $s & ZE(i) nor 100111 $d, $s, $t $d = ~($s | $t) or 100101 $d, $s, $t $d = $s | $t ori 001101 $t, $s, i $t = $s | ZE(i) xor 100110 $d, $s, $t $d = $s ^ $t xori 001110 $t, $s, i $t = $s ^ ZE(i)  MIPS Instructions cont.  Control flow in assembly o Shift instructions o Not all programs follow a linear set of instructions st  1 character: "s" = shift  Some operations require the code to branch to one section of  2 character: "l" or "r" = shift direction the code or another (if/else)  3 character: "a" or "l" = arthematic or logical  Some require the code to jump back and repeat a section of  shift left logical == shift left arthematic (not in use) code again (for/while) th  4 (optional) character: "v" = a variable number of bits, o For this, we have labels on the left-hand side that indicate the points specified by $s that the program flow might need to jump to Instruction Opcode/ Synatx Operation  References to these points in the assembly code are resolved at compile time to offset values for the program counter Function sll 000000 $d, $s, a $d = $t << a o Branch instructions sllv 000100 $d, $t, $s $d = $t << $s  Branch operations are key when implementing if statements and while loops sra 000011 $d, $t, a $d = $t >> a  The labels are memory locations, assigned to each lable at srav 000111 $d, $t, $s $d = $t >> $s compile time srl 000010 $d, $t, a $d = $t >>> a  Note: i is calculated as srlv 000110 $d, $t, $s $d = $t >>> $s (label – (current PC + 4)) >> 2  In branch i-type instruction last 16 bits is the difference btwn  Data movement instructions the branch-call location and the branch-to location o These instructions for operating on the HI and LO registers  beq = branch if equals Instruction Opcode/ Synatx Operation  bgtz = branch if greater than Function  blez = branch if lesser than or equals to mfhi 010000 $d $d = hi  bne = branch if not equals mflo 010010 $d $t = lo  Note: branch if lesser than DNE, instead: beq, bgtz & blez mthi 010001 $s hi = $s Instruction Opcode/Fun Synatx Operation mtlo 010011 $s lo = $s ction beq 000100 $s, $t, label if ($s == $t)  ALU instructions pc += i << 2 o Note that for ALU instructions, most are R-type instructions  The six-digit codes in the table are therefore the function codes 000111 $s, label if ($s > 0) pc += i << 2 (opcodes are 000000)  Exceptions are the I-type instructions (addi, andi, orri, etc.)ez) 000110 $s, label if ($s <= 0) o Note all R-type instructions have an I-type equivalent pc += i << 2  RISC architecures dictate that an operation doesn’t need an bne 000101 $s, $t, label if ($s != $t) instruction if it can be performed through multiple existing pc += i << 2 operations  Ex. addi + div  divi o Jump instructions  j = “jump”, for calling subroutines  Store the immediate value into a register first than divide  similar to GOTO statements  Example program o A fibonacci sequence C code into assembly code  if an assembly program doesn’t have jump statements, 1. int fib(void) { the program will run from beginning to end sequentially 2. int n = 10;  jal = “jump and link”, for calling functions 3. int f1 = 1, f2 = -1; 4. while (n != 0) {  register $ra ($31) stores the address that’s used when 5. f1 = f1 + f2; returning from a subroutine 6. f2 = f1 – f2;  jalr = “jump and link to register” 7. n = n – 1;  jr = “jump to register”, jump to address contain in register $s 8. } 9. return f1;  usually for ending a subroutine by jumping to the return 10. } address stored in $ra  note: jr and jalr are not j-type instructions o In assembly Instruction Opcode/ Synatx Operation  Note: beq, j, and sb instructions will be touched upon later Function 1. # fib.asm j 000010 label pc += i << 2 2. # register usage: $t3=n, $t4=f1, $t5=f2 3. # RES refer to memory address of result jal 000011 label $ra = pc; pc += i << 2 4. FIB: addi $t3, $zero, 10 # initialize n=10 jalr 001001 $s $31 = pc; pc = $s 5. addi $t4, $zero, 1 # initialize f1=1 6. addi $t5, $zero, -1 # initialize f2=-1 jr 001000 $s pc = $s 7. LOOP: beq $t3, $zero, END # done loop if n==0 8. add $t4, $t4, $t5 # f1 = f1 + f2 o Comparison instructions 9. sub $t5, $t4, $t5 # f2 = f1 - f2  Comparison operation stores a: 10. addi $t3, $t3, -1 # n = n – 1  1 in the dst register if the less-than comparison is true 11. j LOOP # repeat until done  0 in the dst register otherwise 12. END: sb $t4, RES # store result Instruction Opcode/ Synatx Operation o Making an asssmbly program Function  Assembly Lanauge programs typically have structure similar to slt 101010 $d, $s, $t $d = ($s < $t) simple Python or C programs sltu 101001 $d, $s, $t $d = ($s < $t)  Set aside registers to store data slti 001010 $t, $s, i $t = ($s < SE(i))  Have sections of instructions that manipulate this data  Its always good to decide at the beginning which registers willsltiu 001001 $t, $s, i $t = ($s < SE(i)) be used for what purpose o If/Else statements in MIPS  Ex. For loop  Strategy for if/else statements;  For loop in C  Test condition, jump to if logic when condition is true 1. for (i = 0; i < 100 ; i++) {  Otherwise, perform else logic block, and jump to first 2. j = j + i; 3. } line after if logic block  Ex. convert an If-Else block in C to assembly  For loop in Assembly 1. # $t0 = i, $t1 = j  If-Else block in C 2. main: add $t0, $zero, $zero # set $t0 to 0 1. if (i == j) { 2. i = i + 1; 3. add $t1, $zero, $zero # set $t1 to 0 3. } else { 4. addi $t9, $zero, 100 # set $t1 to 100 4. j = j - 1; 5. START: beq $t0, $t9, EXIT # branch if (i == 100) 5. } 6. add $t1, $t1, $t0 # j = j + i 6. j = j + i; 7. UPDATE: addi $t0, $t0, 1 # i++ 8. j START  In Assembly – version 1: If block after Else block 9. END: 1. # $t1 = i, $t2 = j  while loops are the same, without the initalizaiton and update 2. main: beq $t1, $t2, IF # branch if (i == j) 3. addi $t2, $t2, -1 # j— sections 4. j END # jump over if 5. IF: addi $t1, $t1, 1 # i++ 6. END: add $t2, $t2, $t1 # j += i  In Assembly – version 2: Else block after If block 1. # $t1 = i, $t2 = j 2. main: bne $t1, $t2, ELSE # branch if (i != j) 3. addi $t1, $t1, 1 # i++ 4. j END # jump over else 5. ELSE: addi $t2, $t2, -1 # j-- 6. END: add $t2, $t2, $t1 # j += i  Use flow charts to sort out the control flow of the code o Ex. multiple If conditions  Multiple If-else in C 1. if (i == j || i == k) { 2. i++; 3. } else { 4. j--; 5. } 6. j = i + k;  In Assembly: branch statement for each condition 1. # $t1 = i, $t2 = j, $t3 = k 2. main: beq $t1, $t2, IF # cond1: branch if (i == j) 3. bne $t1, $t3, ELSE # cond2: branch if (i != k) 4. IF: addi $t1, $t1, 1 # if-body: i++ 5. j END # jump over else 6. ELSE: addi $t2, $t2, -1 # else-body: j-- 7. END: add $t2, $t1, $t3 # j = i + k o Loops in MIPS  Loops usually implemented with the following syntax  For loop in C 1. for ( ; ; ) { 2. 3. }  For loop in Assembly 1. main: 2. START: if (!) branch to EXIT 3. 4. UPDATE: 5. jump to START 6. EXIT:  Week 10 review o Q1 What signals would the control unit turn on and off in order to load a memory value into $t0  PCWrite = 0, PCWriteCond = 0  IorD = 1  MemRead = 1, MemWrite = 0  MemtoReg = 1  IRWrite = 0  PCSource = x  ALUOp = 001 (add)  ALUSrcA = 0, ALUSrcB = 2  RegWrite = 1, RegDst = 0 o Q2 What are the following assembly language instructions doing?  sub $t7, $t0, $t1  Subtract register $t1 from $t0  place the result into $t7  andi $t7, $t0, 15  Bitwise AND between register $t0 and 15 (1112 )  place the result into $t7  sra $t2, $t1, 2  2 bits arithmetic shift of register $t1 to the right  place the result into $t2 o Q3 How do you translate the assembly language instruction add $t7, $t0, $t1 into machine code?  #1. Identify instruction type: r-type instruction   #2. The opcode  For R-type, the opcode = 000000  #3. The function code (R-type only)  add = 100000  #4. The Register values  translate register variables into values  Source registers: $t0 = register 8 = 01000 $t1 = register 9 = 01001  Destination register: $t7 = register 15 = 01111  add $t7, $t0, $t1 in machine code is: 000000 01000 01001 01111 XXXXX 100000 o Q4 same as mar25ce review question  Week 10 review Q5 (from 2011/2012 Winter Part D Q3)  Intracting with Memory o Wirte a short assembly language program that is a translation of o All programs needs to fetch values from memory into registers, program on the right. You can assume that i has been placed on top operate on them, then store the values back into memory of the stack and that the return value should be placed on the stackmory operations are i-type, with the following form as well before returning to the calling program. o sign In C 1. int sign (int i) { 2. if (i > 0) { 3. return 1; 4. } else if (i < 0) { o Load & Store instructions 5. return -1;  i is an offset, usually zero; used for when multiple values are 6. } else { needed from a given memory location 7. return 0; 8. }  for arrays i is used to extract values not at the front of 9. } the arrays pointer to array stored at register $s is the pointer to the first element o sign in Assembly 1. # register usage: $t0=i, $v0=ret_val  for objects, pointer to the start of the object’s block of 2. SIGN: addi $sp, $sp, 4 # move $sp down memory stored at register $s, use i to traverse object  or for stack parameters 3. lw $t0, 0($sp)
More Less

Related notes for CSC258H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit