Thông tin tài liệu
MIPS Assembly Tutorial Types of Instructions • There are 3 main types of assembly instructions – Arithmetic - add, sub, mul, shifts, and, or, etc. – Load/store – Conditional - branches Arithmetic Instructions add a, b, c a = b+c add a, a, d a = d+a = d+b+c add a, a, e a = e+a = e+d+b+c Example: Translate the following instructions to assembly code a = b+c d = a-e Solution: add a, b, c sub d, a, e Arithmetic Instructions Example: Translate the following instructions to assembly code. Remember with RISC, only 1 operation per instruction! HINT - you may need temporary variables f = (g+h) - (i+j) Solution: add t0, g, h add t1, i, j sub f, t0, t1 Operands • In assembly code, you can’t use variables such as a, b, c, etc • In RISC instruction sets, operands must be registers such as r1, r2, r3, etc – r0 is typically reserved to hold the immediate value of 0 – There is a limited number of registers • MIPS has 32 Arithmetic Instructions Using Registers Example: Translate the following instructions to assembly code. Assume that g, h, i, and j are already stored in r1, r2, r3, and r4. Store f in r5 f = (g+h) - (i+j) Solution: add r6, r1, r2 add r7, r3, r4 sub r5, r6, r7 What about more data?? • With only a limited number of registers, not all data can be stored in registers at the same time. – Registers only store data that is currently being operated on • Variables are stored in memory and then loaded into registers when needed using data transfer instructions – Load word (lw) and store word (sw) Load and store word • Load word format – lw destination register, memory location • Store word format – sw source register, memory location • Memory location format – Offset(base address) • Base address = starting location of data in memory • Offset = how far away is location to access from base address – Values are added together LW Example Example: Assume that A is an array of 100 words. Assume the base address is stored in r3, g is in r1 and h is in r2 g = h + A[8] Solution: lw r4, 8(r3) add r1, r2, r4 Offset Base Address This is simplified, more details later… Data in Memory • All variables/data are stored in memory – You will need to do this in your assembler – Your ISS will need a data structure to hold main memory • Array is fine [...]... will do but it will output in binary MIPS -> Machine Language Example: Show the real MIPS language version of the following instruction in both decimal and binary add r0, r1, r2 Solution: decimal 0 0 1 2 0 32 00010 00000 100000 5 bits 6 bits binary 000000 00000 00001 6 bits 5 bits 5 bits 5 bits Each segment is referred to as a field Details to come… MIPS Fields • MIPS fields are giving names to make... an address up to 16bits • Opcode determines the format MIPS Instruction Encoding MIPS Asm -> Machine Language Example: Assume r1 is the base of A and r2 corresponds to h, the C statement: A[300] = h + A[300] is compiled to: lw r0, 1200(r1) add r0, r2, r0 sw r0, 1200(r1) What is the MIPS machine code for these three instructions? (Use figure 3.5) MIPS Asm -> Machine lw r0, 1200(r1) add r0, r2, r0 Language... Calculate the offset using assembly instructions but don’t use multiplication yet (mult instruction is different) g = h + A[i] Solution: add r5, r4, r4 add r5, r5, r5 add r5, r5, r3 lw r6, 0(r5) add r1, r6, r2 # Temp reg r5=2*i # Temp reg r5=4*i # t1 = addr of A[i] (4*i+r3) # Temp reg r0=a[i] # g=h+a[i] Translating MIPS Assm Language to Machine Language • Translate human readable assembly code to machine... sometimes called the function code MIPS Fields • Problem occurs with an instruction needs a longer field than that showed on the previous slide – I.e LW must specify 2 registers and a constant Limited to 5-bit constant if use previous format • Solution: There are different formats for different types of instructions – Previous slide is R-type (R-format): • R=register MIPS Fields op rs rt 6 bits 5 bits... save is in r6 i->r3, j->r4, k->r5 while ( save[i] == k ) i = i+j Solution: Loop: Exit: add r1, r3, r4 add r1, r1, r1 add r1, r1, r6 lw r0, 0(r1) bne r0, r5, Exit add r3, r3, r4 j Loop Other Styles of MIPS Addressing • Constant or immediate operands – Programs often use constant values – I.e incrementing to the next data element while scanning an array • addi instruction - adds an immediate value to . MIPS Assembly Tutorial Types of Instructions • There are 3 main types of assembly instructions – Arithmetic - add, sub, mul, shifts,. following instructions to assembly code a = b+c d = a-e Solution: add a, b, c sub d, a, e Arithmetic Instructions Example: Translate the following instructions to assembly code. Remember with. – There is a limited number of registers • MIPS has 32 Arithmetic Instructions Using Registers Example: Translate the following instructions to assembly code. Assume that g, h, i, and j are
Ngày đăng: 23/10/2014, 16:35
Xem thêm: