MIPS Arithmetic - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

MIPS Arithmetic

Description:

California State ... California State University San Marcos. 3. Review of operators and ... C variables map onto registers, what about large data ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 26
Provided by: ahmadrh
Category:

less

Transcript and Presenter's Notes

Title: MIPS Arithmetic


1
MIPS Arithmetic and Transfer Instructions
2
This program will take input (integer) from
user and print the message input value is
plus the integer that is input by user. By
looking at system call table we will see that
to input an integer, v0 should get the value of
5 and a0 should get the value of v0 for
printing it. .data start of the
data str .asciiz \n\n\n\n\n\t\t enter an
integer str1 .asciiz \n\n\n\n\n\t\t the input
is .text start of the code main
la a0, str li v0, 4 print
the content of a0 which is string
syscall li v0, 5 read an
integer from user into v0 syscall add
t0,0,v0 store vo to t0 la a0, str1
li v0, 4 print the content of
a0 which is string syscall add
a0, t0,0 store the content of t0 to a0
li v0, 1 print the content of a0
which is integer syscall
3
  • Review of operators and operands in C and C
  • Suppose we have following C codes
  • Celsius 5(farh-32)/9
  • a b C d - e
  • In these codes
  • Operands are
  • variables a, b, c, d, e, farh, Celsius
  • Constants5, 32, 9
  • Operators are , -, , /
  • Now in this section we will introduce
    instructions in MIPS and their Operator and
    Operands

4
  • Assembly Design key Concept
  • The goal of designing assembly language for MIPS
    is to keep the instruction format simple.
  • Since MIPS is a reduced instruction set computer
    (RISC), there are limitations on what can be used
    as a variable and what cannot be used
  • Each line of assembly code contains at most one
    instruction

5
  • Addition and Subtraction Instruction Syntaxes
  • Based on the type of operands, there are two kind
    of syntax for additions and subtractions
  • First Case When the operands are variables
    (Registers)
  • Second Case When one of the operands is constant

6
  • Addition and Subtraction
  • First Case When the operands are variables
    (Registers)
  • 1 2 3 4
  • add s0, s1, t1
  • The name of the operation (add)
  • Operand that gets the result (destination
    register)
  • First operand for operations (Source1 register)
  • Second operand for operations(Source2 register)

7
  • Example of Addition
  • Compile this C code
  • a b c
  • to MIPS instruction where registers s0, s1,
    s2, are associated with variables a, b and c,
    respectively
  • add s0, s1, s2
  • Example of Subtraction
  • Subtraction has exactly the same syntax as
    addition
  • Compile this C code
  • d e - f
  • to MIPS instruction where registers s3, s4,
    s5, are
  • associated with variables d, e and f,
    respectively
  • sub s3, s4, s5

8
  • Example
  • Compile the following C statement
  • a b c d e
  • where variables a, b, c, d, and e correspond to
    register s0, s1, s2, s3, and s4
  • This code breaks into multiple instructions
  • add s0, s1, s2 a b c
  • add s0, s0, s3 a a d
  • sub s0, s0, s4 a a e
  • Notice A single line of C code may break into
    several lines of MIPS

9
  • Example
  • Compile this C segment
  • f (g h) (i j)
  • Where f, g, h, i, j correspond to s0, s1, s2,
    s3, and s4
  • Answer
  • add t0, s1, s2 add gh and put it into
    temporary register t0
  • add t1, s3, s4 add ij and put it into
    temporary register t1
  • sub s0, t0, t1 subtract t1 from t0 and
    put the result into s0

10
  • Addition and Subtraction
  • Second Case When one the operand is an immediate
    value
  • Immediates are numerical constants
  • They appear often in code, so there are special
    instruction for them
  • 1 2 3 4
  • addi s0, s1, 10
  • The name of the operation (add immediate)
  • Operand that gets the result (destination
    register)
  • First operand for operations (source1 register)
  • Second operand for operations (constant value)

11
  • Example
  • Compile the following C code
  • f g 12
  • to MIPS assembly language instructions where f
    corresponds to s0 and g corresponds to s1
  • addi s0, s1, 12
  • Example
  • Compile the following C code
  • f g - 12
  • to MIPS assembly language instructions where f
    corresponds to s0 and g corresponds to s1
  • addi s0, s1, -12

12
  • Register zero
  • One particular immediate is register zero that
    appears very often in code.
  • So we define register zero, (0) to always have
    the value 0
  • Example Compile the C code
  • f g
  • Where register s0, s1, are associated with
    variables f and g, respectively
  • add s0, s1, zero
  • Note You cannot change the value of resister zero

13
  • Using Complex data Structures (Arrays)
  • Programming languages have simple variables that
    contain single data elements as shown in the
    previous example
  • They also have more complex data structures like
    arrays
  • This complex data structure can contain many more
    elements than there are registers in the machine
  • So, how can the computer represent and access
    such a large structure ?
  • C variables map onto registers, what about large
    data structures like arrays?
  • The processor can keep only a small amount of
    data in registers but computer memory contains
    millions of data elements so data structures like
    arrays are kept in memory.

14
  • MIPS includes instructions that transfer data
    between memory and registers. Such instructions
    are called data transfer instructions
  • Since MIPS arithmetic instructions only operate
    on registers and never directly on memory, MIPS
    include instructions that transfer data between
    memory and registers
  • So first we will go through some facts about MIPS
    memory architecture and then we will introduce
    data transfer instructions.

15
  • Memory Organization of MIPS
  • The purpose of the memory is to store groups of
    bits and deliver them (to the processor) for
    loading into registers upon demand
  • Most recent computers store information in
    multiples of 8-bits
  • In addition, most computers also assign a numeric
    address to each byte (8-bits)
  • Memory addresses are 32-bit numbers ranging from
    0x00000000 to 0xFFFFFFFF.
  • For each running program, data and instructions
    are located in different fixed blocks of memory

16
  • The SPIM simulator always assigns your program to
    the fixed even number locations for your
    convenience
  • 0x00400000 Text segment program instructions
  • 0x10000000 Data segment
  • 0x7FFFFFFF is stack segment
  • A word generally means the number of bits that
    can be transferred at one time on the data bus
    and stored in a register
  • In the case of MIPS, a word is 32 bits, that is 4
    bytes
  • Words are always stored in 4 consecutive bytes in
    memory. Starting with an address that is
    divisible by 4

17
  • To access a data in memory, the instruction must
    have the memory address.
  • Memory is just a large single dimensional array
    with addresses acting as index to that array

. . .
. . .
100
Ox00001100 12
10
Ox00001000 8
101
Ox00000100 4
1
Ox00000000 0
Address
Data
Processor
Memory
18
  • Data Transfer Instructions
  • Load Instruction Syntax
  • 1 2 3 4
  • lw t0, 8 (s0)
  • The name of the operation (load word from memory
    to register)
  • Register that will receive value from memory
  • A numerical constant (offset) that tells us how
    many memory location we should add to or subtract
    from pointer
  • Register containing pointer to certain block of
    memory

19
  • Example
  • lw t0, 12 (s0)
  • This instruction will find out what is the
    address inside register s0 (pointer)
  • Then, it adds that address to 12 (offset) and
    then loads the content of the memory12s0 to
    t0
  • Notes
  • s0 is called the base register
  • 12 is offset
  • Offset is generally used in accessing elements of
    memory
  • Base register points to beginning of certain
    block of memory

20
  • Example
  • Lets assume that A is an array of 100 words and
    that the compiler has associated the variable g
    to register s1. Lets also assume the starting
    address or base address of the array is in s3.
    Translate this C statement
  • g A9
  • In order to compile this statement, we need to
    transfer A9 from memory to register s1.
  • We need to use the load instruction,
  • Need to find out the offset
  • Need to have the base register (s3)
  • Need to have register to load ninth element of
    array into it (s1)
  • To find out offset
  • Since each word takes four place in memory, the
    offset value is 9436
  • So the compiled instruction is
  • lw s1, 36 (s3)

21
  • Example
  • f (ij)-A12
  • Assume that variable f is associated with
    register s2 and variable i and j to register
    s0, s1 and the base address of the array is in
    s3. What is MIPS assembly code?
  • First load A12 from memory to t0 temporary
    register
  • lw t0 , 48(s3)
  • Add ij and put it into temporary register t1
  • add t1, s0, s1
  • Subtract t0 from t1 and put the result back
    in register s2
  • sub s2, t1, t0

22
  • Store Instruction Syntax
  • We also want to store value from register into
    memory
  • Store instruction syntax is identical to load
    instruction syntax
  • Store Instruction Syntax
  • 1 2 3 4
  • sw t0, 8 (s0)
  • The name of the operation (store word from
    register to memory)
  • Register that its content needs to be stored in
    the memory
  • A numerical constant (offset) that tells us how
    many memory
  • Location we should add to or subtract from
    pointer
  • Register containing pointer to certain block of
    memory

23
  • Example
  • sw t0, 12(s0)
  • This instruction first finds out the memory
    location that the content of register needs to be
    stored by adding 12 byte offset to the pointer
    that is in s0
  • Then store the content of register t0 to the
    memorys0 12

24
  • Example
  • Assume variable h is associated with 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
  • A12 h A8
  • Now two of the operands are in memory.
  • So we need several MIPS instructions to perform
    the above single C code
  • 1) Store A8 from memory to temporary register
    t0
  • lw t0, 32(S3) 32 is 84
  • 2) add t0, s2, t0 t0 gets the result of
    hA8
  • 3) We should store the result of addition from
    t0 to memory A12
  • sw t0, 48 (s3) 48 is 124

25
  • Example
  • Assume variables g, h and i are associated with
    register s1, s2, and s4 and the base address
    of the array A is in s3. What is the MIPS
    assembly code for the C assignment statement
    below
  • g h A i
  • We need to load A i in to temporary register
    t0 , then add t0 to s2 (h) and store the
    result in to s1.
  • load Ai from memory to temporary register t0
  • add t1, s4, s4 Temp Reg t1 2 i
  • add t1, t1, t1 Temp Reg t1 4 i
  • add t1, t1, s3 t1 address 4i s3
  • lw t0, 0(t1) t0 A i
  • add A i to h and place it in g
  • add s1 , s2, t0 g h A i
Write a Comment
User Comments (0)
About PowerShow.com