Chapter 2.1- 2.5 & Quizzes 1 - 3.
How is the sw instruction identified in MIPS using decimal numbers?
43
What is a stack?
A data structure for spilling registers organized as a last-in-first-out queue.
What is the decimal value of this 64-bit two's complement number? 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1000? A)-4 B)-8 C)-16 D)18,446,744,073,709,551,609
B) -8
True/False: Immediate operands can be represented by up to 31 bits.
False
True/False: The C Programming Language allows for variable typing to be implicit - that is not all variables, such as those used in loops, need to be declared.
False
True/False: The forms ++a and a++ have the exact same effect and are stylistic concerns for the programmer.
False
True/False: Logical shifts to the right always sign fill the digits shifted in from the left.
False.
True/False: The sign bit is multiplied by 2^31, and the rest of the bits are then multiplied by the negative versions of their respective base values.
False: The sign bit is multiplied by -2^31, and the rest of the bits are then multiplied by the positive versions of their respective base values.
What must the data transfer instruction supply to access a word in memory?
Must supply the memory address. address: A value used to delineate the location of a specific data element within a memory array.
In MIPS assembly language, registers $s0 to $s7 map onto what registers?
Registers 16 to 23.
The variable name for an array contains?
The address of the first member of the array.
What is global pointer ($gp)?
The register that is reserved to point to the static area.
MIPS assembly language includes an instruction just for the procedures: it jumps to an address and simultaneously saves the address of the following instruction in register $ra. What is this?
jump-and-link instruction or jal jal ProcedureAddress
What happens when you forget to free up space in memory?
memory leak
Where do arithmetic operation occur?
on Registers in MIPS instructions
What is the register $ra used for in MIPS?
one return address register to return to the point of origin.
What is pop?
remove element from stack.
void strcopy (char x[], char y[]){ int i; i-0; while ((x[i] = y[i]) != '\0') i+=1 } in MIPS if the base address for arrays x and y are found in $a0 and $a1, while i is found in $s0.
strcpy: addi $sp, $sp, -4 #adjust stack for 1 more item sw $s0, 0($sp) #save $s0 add $s0, $zero, $zero #i = 0 + 0 L1: add $t1, $s0, $a1 #address of y[i] in $t1 lbu $t2, 0($t1) #$t2 = y[i] add $t0, $s0, $a0 #address of x[i] in $t3 sb $t2, 0($t3) #x[i]=y[i] beq $t2, $zero, L2 #if y[i] == 0, go to L2 addi $s0, $s0, 1 #i = i+1 j L1 L2: lw $s0, 0($sp) #y[i]==0; end of string. Restore old $s0. add $sp, $sp, 4 #pop 1 word off the stack jr $ra #return
How many registers are in the MIPS architecture?
32
There are how many general purpose registers in the MIPS architecture?
32
What is the size of a register in the MIPS architecture?
32 bits
How is the lw instruction identified in MIPS using decimal numbers?
35
What are the argument registers?
$a0-$a3
What registers are saved that must be preserved on a procedure call (if used, the callee saves and restores them)?
$s0 to $s7 saved
What is the stack pointer register?
$sp $sp is itself preserved by the callee adding exactly the same amount that was subtracted from it; and the other registers are preserved by saving them on the stack (if they are used) and restoring them from there.
To test to see if the register $s2 is less than the constant 10 we write... slti $t0, $s2, 10 If the register $s2 is indeed less than the constant 10, what value is stored into the register $t0?
$t0 = 1, if $s2 < 10
What registers are not preserved by the callee (called procedure) on a procedure call?
$t0 to $t9 temporary
What are the return registers?
$v0-$v1
What does lbu do in MIPS?
(load byte unsigned) works with unsigned integers.
What does lb do in MIPS?
(load byte) treats the byte as a signed number and thus sign-extends to fill the 24 left-most bits of the register.
What is a stack pointer?
A value denoting the most recently allocated address in a stack that shows where registers should be spilled or where old register values can be found. In MIPS it is register $sp.
Suppose we have a data type for 6-bit 2's-complement numbers. What are the smallest negative and largest negative values that could be represented? A) -2^(6-1) to (2^(6-1)) -1 B) -2^6 to (2^6) -1 C) 0 to (2^6) -1 D) -(2^(6-1)) + 1 to 2^(6-1)
A) -2^(6-1) to (2^(6-1)) -1
What is the jump and link instruction?
An instruction that jumps to an address and simultaneously saves the address of the following instruction in a register ($ra in MIPS)
What is an conditional branch?
An instruction that requires the comparison of two values and that allows for subsequent transfer of control to a new address in the program based on the outcome of the comparison.
What is $at?
Assembler temporary. Reserved for the assembler.
What are automatic variables?
Automatic variables are local to a procedure and are discarded when the procedure exits.
Convert 1111 1101 0001 from two's complement to decimal. A) -45 B) -47 C) 47 D) -2,368,347,625
B) -47
What is memory like?
Large single-dimensional array, with the address acting as the index to that array, starting at 0.
Here is a traditional loop in C: while(save[i] == k){ i += 1; } Assume that i and k correspond to registers $s3 and $s5 and the base of the array save is in $s6. What is the MIPS assembly code corresponding to this C segment?
Loop: sll $t1, $s3, 2 #Temp reg $t1 = i*4 add $t1, $t1, $s6 #$t1 = address of save[i] To get the address of save[i], we need to add $t1 to the base of save in $s6. lw $t0, 0($t1) #Temp reg $t0 = save[i] Use the address to load save[i] into temp register. bne $t0, $s5, Exit # go to Exit if save[i] != k loop test addi #s3, $s3, 1 #i = i +1 add 1 to i j Loop #go to loop Exit: #Exit
Where are data structures (arrays and structures) kept?
Memory
What are leaf procedures?
Procedures that do not call others are called leaf procedures.
In MIPS the add instruction requires how many variables?
Three.
In MIPS how many instructions does it take to add four variables together?
Three: ex: variables: a, b, c, d, e adding together: b, c, d, e. add a, b, c add a, a, d add a, a, e
What is the purpose of a bus in a computer?
Transfer data and instructions between components of a computer.
True/False: Bitwise operators other than shifts only effect the bits in the exact same positions in each of the operands.
True
True/False: MIPS supports negative constants, so there is no need for subtract immediate in MIPS.
True
True/False: One major difference between logical and arithmetic operations is that one extends with 0's and the other with the sign of the value.
True.
True/False: The format of MIPS instructions varies, but the length stays the same.
True.
True/False: Unsigned loads simply fill with 0s to the left of the data, since the number represented by the bit patter is unsigned.
True.
True/False: Twos complement has one negative number, that has no corresponding positive number.
True. -2,147,483,648 (-2^31)
What is put into the funct and opt fields in combination to tell the MIPS computer that the instruction performs addition? subtraction?
addition: 0 in op, 32 in funct. subtraction: 0 in op, 34 in funct.
What is PC-relative addressing?
an addressing regime in which the address is the sum of the program counter and a constant in the instruction. program counter = register + branch address
Does MIPS use big-endian or little-endian?
big-endian
Each memory address refers to a specific?
byte
What are the registers $a0-$a3 used for in MIPS?
four argument registers in which to pass parameters.
In C: int leaf_example(int g, int h, int i, int j) { int f; f = (g+h) - (i+j) return f; } What would it look like in MIPS? $sp is stack pointer.
g, h, i and j correspond to the argument registers $a0 - $a3, and f corresponds to $s0. The compiled program starts with the label of the procedure. leaf_example: addi $sp, $sp, -12 #adjust stack to make room for 3 items sw $t1, 8($sp) #save register $t1 for use afterwords sw $t0, 4($sp) #save register $t0 for use afterwords sw $s0, 0($sp)#save register $s0 for use afterwords add $t0, $a0, $a1 #register $t0 contains g+h add $t1, $a2, $a3 #register $t1 contains I+j sub $s0, $t0, $t1 #f = $t0 - $t1 (f = g+h - i+j) add $v0, $s0, $zero #returns f ($v0 = $s0 + 0) restore the three old values of the registers we saved by "popping" them off the stack. lw $s0, 0($sp) #restore register $s0 for caller lw $t0, 4($sp) #restore register $t0 for caller lw $t1, 8($sp) #restore register $t1 for caller addi $sp, $sp, 12 #adjust stack to delete 3 items jr $ra #jump back to calling routine
How do you copy a half word in MIPS?
lhu $t0, 0($sp) #read half word (16 bits) from source sh $t0, 0($gp) #write half word (16 bits) to destination
What does lh do?
load half: loads a half word from memory placing it in the right most 16 bits of a register. Like load byte (lb) lh treats the halfword as a signed number, and thus sign extends to fill the 16 left most bits of the register. lhu deals/works with unsigned integers.
How is the sll instruction identified in MIPS using decimal numbers?
0 in op field, 0 in func field. for example: sll $t2, $s0, 4 (see image)
If you were to shift 9 left by four, what would be the value?
0000 1001 = 9 shift left by 4.. 1001 0000= 144 This is the equivalent of multiplying 9 by 16.
What are two ways to use a constant as a operand? For example, to add the constant 4 to register $s3.
1. lw $t0, AddrConstant4($s1) #$t0=constant 4 add $s3, $s3, $t0 #$s3= $s3 + $t0 ($t0=4) 2. addi $s3, $s3, 4 #$s3 = $s3 + 4
What are considered three popular instruction sets?
1. ARMv7: similar to MIP, most popular in the world. 2. Intel x86: powers both the PC and the cloud of the Post PC era. 3. ARMv8: extends address size of ARMv7 from 32 bits -> 64 bits (closer to MIPS than ARMv7).
In the execution of a procedure what 6 steps must the program follow?
1. Put parameters in place where the procedure can access them. 2. Transfer control to the procedure. 3. Acquire the storage resources needed for the procedure. 4. Perform the desired task. 5. Put the result value in a place where the calling program can access it. 6. Return control to the point of origin, since a procedure can be called from several points in a program.
What does a compiler do?
1.Translates a computer program into machine language. 2.Checks a program for syntax errors.
What is the MIPS code to load this 32 bit constant into register $s0? 0000 0000 0011 1101 0000 1001 0000 0000
1.load the upper 16 bits, which is 61 in decimal lui $s0, 61 #61 = 0000 0000 0011 1101 value in $s0 after is: 0000 0000 0011 1101 0000 0000 0000 0000 insert the lower 16 bits, which is 2304 in decimal ori $s0, $s0, 2304 #2304 = 0000 1001 0000 0000 now $s0 is... 0000 0000 0011 1101 0000 1001 0000 0000
MIPs fields are given names to make them easier to discuss. What are they? (R-type)
1.op: Basic operation of the instruction, traditionally called the opcode. 2.rs: The first register source operand. 3.rt: The second register source operand. 4.rd: The register destination operand. It gets the result of the operation. 5.shamt: Shift amount. 6.funct: Function. This field, often called the function code, is used together with op to select an arithmetic instruction. Register-to-register arithmetic instructions use the R-type format.
what is an instruction format?
A form of representation of an instruction composed of fields of binary numbers.
What is assembly language?
A human-readable form of machine language
What is a return address?
A link to the calling site that allows a procedure to return to the proper address (in MIPS it is stored in register $ra)
Within a computer's CPU, what is a register?
A place to store data that is to be manipulated by program instructions.
What is a callee?
A procedure that executes a series of stored instructions based on parameters provided by the caller and then returns control to the caller.
What is a basic block?
A sequence of instruction without branches, except possibly at the end, and without branch targets or branch labels, except possibly at the beginning.
What is a procedure?
A stored subroutine that performs a specific task based on the parameters with which it is provided.
What is a frame pointer?
A value denoting the location of the saved registers and local variables for a given procedure.
What is a push?
Add element to stack.
Two's complement representation of negative numbers always has what in the most significant bit?
All negative numbers have a 1 in the most significant bit.
What does malloc() do?
Allocates space on the heap and returns a pointer to it.
What is a procedure frame?
Also called activation record, the segment of the stack containing a procedure's saved registers and local variables.
The command: sll $t0, $t1, 4 does... A)Multiplies the value in $t0 by 16 and puts it in $t1 B) Multiplies the value in $t1 by 16 and puts it in $t0 C)Adds 4 to the value in $t1 and puts it in $t0 D)Multiplies the value in $t1 by 4 and puts it in $t0
B) Multiplies the value in $t1 by 16 and puts it in $t0
What is machine language?
Binary representation used for communication within a computer system.
a = a + b; compiles to the following MIPS instructions: A) add $s2, $s1, $s1 B) add $t1, $s1, $s2 C) add $s1, $s1, $s2 D) add $s1, $s2, $s3
C) add $s1, $s1, $s2
C Code for: bne $s3, $s4, Else add $s0, $s1, $s2 j Exit Else: sub $s0, $s1, $s2 Exit: ... Means... A) if (i>=j) f = g+h; else f = g-h; B) if (i==j) f = g-h; else f = g+h; C) if (i==j) f = g+h; else f = g-h; D) if (i!=j) f = g+h; else f = g-h;
C) if (i==j) f = g+h; else f = g-h;
The instruction: beq rs, rt, L1 Means... A) if (rs != rt) branch to instruction labeled L1; B) unconditional jump to instruction labeled L1 C) if (rs == rt) branch to instruction labeled L1 D) if (rs >= rt) branch to instruction
C) if (rs == rt) branch to instruction labeled L1
bne $t3, $zero, loop1 means... A) Stores 0 in $3 and branches to loop1 B) Branches to loop1 if $t3 holds the value 0 C)Branches to loop1 if $t3 holds other than the value 0 D)Branches to Loop1 only if $t3 is negative
C)Branches to loop1 if $t3 holds other than the value 0
What is the purpose of the linker?
Combine object files into an executable file.
My C program contains the declaration: char c = 'N' - 'g' + 'a'; What is the value of variable c (as a character)? A) 57 B) 'h' C) 'T' D) 'H'
D) 'H'
Convert 345 to binary. A) 0001 1010 1001 B) 1110 1010 0111 C) 0001 0101 1011 D) 0001 0101 1001
D) 0001 0101 1001
On an Intel computer, a variable of type int is stored at address 0x100. The contents of that part of memory is as shown below. What integer value is stored in variable x. Address Contents 0x100 0x08 0x101 0x5B 0x102 0xE7 0x103 0x03 A) E703085B B) 08035BE7 C) 085BE703 D) 03E75B08
D) 03E75B08
Which is not a valid method for addressing the second element in an array defined as: int bob[10]; A) *(bob+1) B) bob[1] C) *(++bob) D) bob[2]
D) bob[2]
b = d + f[6]; The first line of assembly compiled for this is: A) lw $t0, 6($s3) B) lw $t0, $t3, 6 C) lw $s1, 24($s3) D) lw $t0, 24($s3)
D) lw $t0, 24($s3)
What instructions transfer data between memory and the registers?
Data Transfer instruction: Command that moves data between memory and registers.
True/False: The op code is the operand for the instruction.
False
What does it mean if my C program has an unresolved reference?
The linker cannot find a function that is used by the program.
What are the different types of instruction formats used in MIPS? And what are they used for?
R-format/R-type: Register-to-register arithmetic instructions I-format/I-type: Load, store, branch and immediate instructions all use the I-type format. J-format/J-type: The jump instruction uses the J-type instruction format.
In MIPS assembly language, registers $t0 to $t7 map onto what registers?
Registers 8 to 15.
what does free() do?
Releases space on the heap to which the pointer points.
What is a good negation shortcut when working with two's complement numbers?
Simply invert every 0 to 1, and every 1 to 0, then add one to the result.
What are static variables?
Static variables exist across exits from and entries to procedures. C variables declared outside all procedures are considered static, as are any variables declared using the keyword static. To simplify access to static data, MIPS software reserves another register, called global pointer. ($gp)
what does lhu do?
Store half. Takes a half word from the rightmost 16 bits of a register and writes it to memory.
What is a good sign extension shortcut when working with two's complement numbers?
Take the most significant bit from the smaller quantity- the sign bit- and replicate it to fill the new bits of the larger quantity. The old non-sign bits are simply copied into the right portion of the new word.
What is the function and purpose of a signed load?
The function of a signed load is to copy the sign repeatedly to fill the rest of the register, sign-extension. The purpose is to place a correct representation of the number within that register.
What is the stored-program concept?
The idea that instructions and data of many types can be stored in memory as numbers, leading to the stored-program computer.
What is the most significant bit?
The leftmost bit in a MIPS word.
What is a word?
The natural unit of access in a computer, usually a group of 32 bits; corresponds to the size of a register in the MIPS architecture.
What is a caller?
The program that instigates a procedure and provides the necessary parameter values.
What is a program counter (PC)?
The register containing the address of the instruction in the program being executed.
What is the least significant bit?
The rightmost bit in a MIPS word.
What is the text segment?
The segment of a UNIX object file that contains the machine language code for routines in the source file.
What is an instruction set?
The vocabulary of commands understood by a given architecture.
What happens when you free up space too early in memory?
dangling pointers
Assume the variable h is associated with the register $s2 and the base address of the array A is in $t1. What is the MIPS assembly code for the C assignment statement below? A[300]= h + A[300]; Then represent this in machine language instruction using decimal numbers...
lw $t0, 1200($t1) #temporary reg $t0 gets A[300] add $t0, $s2, $t0 #temporary reg $t0 gets h+A[300] sw $t0, 1200($t1) #Stores h+A[300] back into A[300]
Assume the variable h is associated with the register $s2 and the base address of the array A is in $s3. What is the MIPS assembly code for the C assignment statement below? A[12]= h + A[8];
lw $t0, 32($s3) #temporary reg $t0 gets A[8] add $t0, $s2, $t0 #temporary reg $t0 gets h+A[8] sw $t0, 48($s3) #Stores h+A[8] back into A[12]
In C: f = (g+h) - (i+j) What would it look like in MIPS?
the variables f, g, h, i and j aer assigned to the registers $s0, $s1, $s2, $s3 and $s4. add $t0, $s1, $s2 #register $t0 contains g+h add $t1, $s3, $s4 #register $t1 contains i+j sub $s0, $t0, $t1 #f gets $t0 - $t1, which is (g+h) - (i+j)
What are the registers $v0-$v1 used for in MIPS?
two value registers in which to return values.