Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Coal W3-4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Outline

1. Signed and Unsigned Numbers


2. Representing Instructions in the Computer
3. Arithmetic Instructions in MIPS
4. Logical Instructions in MIPS
Signed and Unsigned Numbers
• Digital systems can handle both positive and negative numbers.
• int, unsigned int, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t,
int32_t, and int64_t
• A signed binary number consists of both sign and magnitude
information.
• Sign indicates whether a number is positive or negative
• Magnitude is the value of the number
Sign and Magnitude Form
• What and Where is Sign Bit?
• The left-most bit in a signed binary number
• It tells you whether the number is positive or negative
• A 0 sign bit indicates a positive number
• A 1 sign bit indicates a negative number
Sign-Magnitude Form
Binary Representation (Base ?)
● Analogy: Just like we use base 10 because we have 10 fingers,
computers use base 2 because their hardware can detect only two
states: on and off.
● Examples: For instance:
○ 1011 in binary is calculated as Decimal?
■ 11 in decimal.
○ Try a few conversions for practice.
Representing Instructions in the Computer

Instructions as Numbers
In a computer, instructions are represented as electronic signals, which can be
interpreted as binary numbers. The components of each instruction (like the
operation type, registers involved, etc.) are represented as distinct numbers. Putting
these parts together as a single series of numbers makes up the complete instruction,
ready for the computer to process.
Mapping Registers to Numbers
In MIPS, registers (which are like small storage locations inside the CPU)
have names for easier reference in assembly code, such as $s0, $t0, etc.
However, each register name has an associated number that the machine
understands:
The $s registers are for "saved" values and map to numbers 16 to 23. For instance:
● $s0 is register 16.
● $s1 is register 17.
● This pattern continues up to $s7, which is register 23.
The $t registers are for "temporary" values and map to numbers 8 to 15:
● $t0 is register 8.
● $t1 is register 9, and so on, until $t7, which is register 15.
Why This Mapping is Important
• This numbering system allows MIPS instructions to directly refer to
registers by number, which makes encoding the instructions into binary
numbers (machine code) straightforward.

• By using a standard numbering system, programmers and compilers can


reliably write instructions that the hardware will interpret in the same way
every time.
Registers in MIPS ISA
Example: Translating a MIPS Assembly
Instruction into a Machine Instruction
add $t0,$s1,$s2 MIPS assembly instruction.
This means, "add the values in registers $s1 and $s2, and place the result in $t0."

● The assembly instruction can be represented by a series of decimal numbers.

Decimal Representation?
Example: Translating a MIPS Assembly
Instruction into a Machine Instruction
add $t0,$s1,$s2 MIPS assembly instruction.

Decimal Representation:
Example: Translating a MIPS Assembly
Instruction into a Machine Instruction
add $t0,$s1,$s2 MIPS assembly instruction.

Decimal Representation:

Binary Representation:

000000 10001 10010 01000 00000 100000


MIPS into a Machine Instruction
add $t0,$s1,$s2

• Each of these segments of an instruction is called a field.


• First and last fields (containing 0 and 32) in combination tell the MIPS computer
that this instruction performs addition.
• Second field is the number of the register that is the first source operand of the
addition operation (17 = $s1)
• Third field gives the other source operand for the addition (18 = $s2)
• Fourth field contains the number of the register that is to receive the sum (8 =
$t0)
• Fifth field is unused in this instruction, so it is set to 0.
• What is this instruction performs?
• This instruction adds register $s1 to register $s2 and places the sum in register $t0.
MIPS into a Machine Instruction
• This instruction can also be represented as fields of binary numbers:
add $t0,$s1,$s2
Representing Instructions in the Computer
• What is instruction format?
• This layout of the instruction is called the instruction format.
• How many bits a MIPS instruction take?
• This MIPS instruction takes exactly 32 bits—the same size as a data word.
• Do all MIPS instructions take 32-bits?
• Yes, all MIPS instructions are 32 bits long.
• For easy of reading these binary instruction, hexadecimal
representation can be used.
Representing Instructions in the Computer
Binary instructions are long and tedious to read and write, so
hexadecimal (base 16) is often used as a more compact representation.
In hexadecimal, each 4-bit group is converted to a single hexadecimal
digit.

Hexadecimal- Binary Conversion: Each hexadecimal digit (0 to F)


corresponds to a 4-bit binary pattern, making conversion quick and easy. For
example:
● 0_hex = 0000_two, 1_hex = 0001_two, and so forth up to F_hex = 1111_two.
Representing Instructions in the Computer
Representing Instructions in the Computer

• How many types of instructions in MIPS?


• Two types of instructions: R-Type and I-Type
• First type with format above is called R-type (for register) or R-format.
• Second type is called I-type (for immediate) or I-format and is used by the
immediate and data transfer instructions.
Representing Instructions in the Computer
I-Type Instruction
Representing Instructions in the Computer
• Let’s look at the load word instruction below:
lw $t0,32($s3) # Temporary reg $t0 gets A[8]
• What should be placed in machine instruction format?

• $s3 = 19 and is placed in the rs field


• $t0 = 8 is placed in the rt field
• 32 is placed in the address field.
• Note that the meaning of the rt field has changed for this instruction:
• in a load word instruction, the rt field specifies the destination register, which receives the
result of the load.
Representing Instructions in the Computer
• How the hardware distinguished between R-type and I-type?
• Each format is assigned a distinct set of values in the first field (op)
• Thus that the hardware knows whether to treat the last half of the instruction
as three fields (R-type) or as a single field (I-type).
• What is common in R-type and I-type instruction?
• The first three fields of the R-type and I-type formats are the same size and
have the same names
• What is different in R-type and I-Type instruction?
• the length of the fourth field in I-type is equal to the sum of the lengths of the
last three fields of R-type
Translating MIPS into Machine Instruction
• First represent the machine language instructions using decimal
numbers for ease.
• The three machine language instructions are:
Translating MIPS into Machine Instruction

• First Field🡪lw = 35 opcode


• Second Field🡪 source register $t1 = 9
• Third Field🡪 destination register $t0 = 8
• Fourth Field🡪 constant offset = 1200
• because A[300] (1200 = 300 x 4)
Translating MIPS into Machine Instruction
• For add instruction
• First Field🡪add = 0 opcode
• Second Field🡪 first source register $s2 = 18
• Third Field🡪 second source register $t0 = 8
• Fourth Field🡪 destination register $t0 = 8
• Fifth Field🡪 shamt = 0
• Sixth Field🡪fucnt = 32 for add
Translating MIPS into Machine Instruction
• First Field🡪sw = 43 opcode
• Second Field🡪 source register $t0 = 8
• Third Field🡪 destination register $t1 = 9
• Fourth Field🡪 constant offset = 1200
• because A[300] (1200 = 300 x 4)
Translating MIPS into Machine Instruction
• Note the similarity of the binary representations of the first and last
instructions.
• The only difference is in the third bit from the left , which is
highlighted here.

You might also like