Professional Documents
Culture Documents
4c. ISA - Design
4c. ISA - Design
- II
Answer: Compute the factorial of the variable num.
C
int prod = 1;
Modifiers Mechanism
* We can add the following modifiers to an instruc6on * The processor internally converts a 16 bit immediate to a 32
that has an immediate operand bit number
* Modifier : * It uses this 32 bit number for all the computa6ons
* default : mov → treat the 16 bit immediate as a signed number * Valid only for arithme6c/logical insts
(automa6c sign extension)
* (u) : movu → treat the 16 bit immediate as an unsigned number * We can control the genera6on of this 32 bit number
* (h) : movh → leA shiA the 16 bit immediate by 16 posi6ons * sign extension (default)
* treat the 16 bit number as unsigned (u suffix)
* load the 16 bit number in the upper bytes (h suffix)
3 4
1
More about Modifiers Examples
* Move : 0x FF FF A3 2B in r0
* default : mov r1, 0xAB 12
mov r0, 0xA32B
sign bit AB 12
* Move : 0x 00 00 A3 2B in r0
* unsigned : movu r1, 0xAB 12
movu r0, 0xA32B
00 00 AB 12
* Move : 0x A3 2B 00 00 in r0
* high: movh r1, 0xAB 12
movh r0, 0xA32B
AB 12 00 00
5 6
function foo
7 8
2
Implementing Functions - II Notion of the Return Address
callee
caller
* To call a func6on, we need to set :
p function call
* pc ← A
p+4 return address
* We also need to store the loca6on of the pc that we need to
come to aAer the func6on returns
ret
* This is known as the return address
* PC of the call instruc6on → p
* We can thus call any func6on, execute its instruc6ons, and
`then return to the saved return address * PC of the return address → p + 4
because, every instruc6on takes 4 bytes
9 10
11 12
3
Register Spilling Spilling
* The no6on of spilling
* The caller can save the set of registers its needs Caller Caller
Restore registers
* The callee saves, the registers, and later restores them
(a) Caller saved (b) Callee saved
13 14
15 16
4
How to use activation blocks ? Using Activation Blocks - II
* Assume caller saved spilling * In the called func6on
* Before calling a func6on : spill the registers * Read the arguments and transfer to registers (if required)
* Save the return address if the called func6on can call other
* Allocate the ac6va6on block of the callee func6ons
* Write the arguments to the ac6va6on block of the callee, if * Allocate space for local variables
they do not fit in registers * Execute the func6on
* Call the func6on * Once the func6on ends
* Restore the value of the return address register (if required)
* Write the return values to registers, or the ac6va6on block of the
caller
* Destroy the ac6va6on block of the callee
17 18
* and return to the caller * These blocks need to be dynamically created and destroyed
– millions of 6mes
* The caller :
* What is the correct way of managing them, and ensuring
* Retrieve the return values from the registers of its ac6va6on their fast crea6on and dele6on ?
block
* Is there a paeern ?
* Restore the spilled registers
* con6nue ...
19 20
5
Pattern of Function Calls Pattern of Function Calls
test test2
* Last in First Out
main foo foobar foobarbar
* Use a stack to store ac6va6on blocks
test2() {
main() {
test();
test() {
... ...
Stack
foo(); test2(); return; foo foo foo foo
} return; }
}
foobar foobar foobar
foo() { foobar() { foobarbar() {
... ... ...
foobar(); foobarbar(); foobarbar
return; return; return;
} } }
6
call and ret instructions Recursive Factorial Program
* ret instruc6on
* Puts ra in pc
25 26
7
Encoding Instructions Basic Instruction Format
5 27
* Encode the SimpleRisc ISA using 32 bits. opcode rest of the instruction
* We have 21 instruc6ons. Let us allot each instruc6on an
unique code (opcode)
Inst. Code Format Inst. Code Format
add 00000 add rd, rs1, (rs2/imm) lsl 01010 lsl rd, rs1, (rs2/imm)
Instruction Code Instruction Code Instruction Code sub 00001 sub rd, rs1, (rs2/i mm) lsr 01011 lsr rd, rs1, (rs2/imm)
add 00000 not 01000 beq 10000 mul 00010 mul rd, rs1, (rs2/imm) asr 01100 asr rd, rs1, (rs2/imm)
sub 00001 mov 01001 bgt 10001 div 00011 div rd, rs1, (rs2/imm) nop 01101 nop
mod 00100 mod rd, rs1, (rs2/imm) ld 01110 ld rd.imm [rs1]
mul 00010 lsl 01010 b 10010
cmp 00101 cmp rs1, (rs2/imm) st 01111 st rd. imm[rs1]
div 00011 lsr 01011 call 10011 and 00110 and rd, rs1, (rs2/imm) beq 10000 beq offset
mod 00100 asr 01100 ret 10100 or 00111 or rd, rs1, (rs2/imm) bgt 10001 bgt offset
cmp 00101 nop 01101 not 01000 not rd, (rs2/imm) b 10010 b offset
and 00110 ld 01110 mov 01001 mov rd, (rs2/imm) call 10011 call offset
or 00111 st 01111 ret 10100 ret
29 30
5 27
8
3-Address Instructions Register Format
32
* Instruc6ons – add, sub, mul, div, mod, and, or, lsl, lsr, asr
opcode 0 dest reg src reg1 src reg2
* Generic 3 address instruc6on op I rd rs1 rs2
* <opcode> rd, rs1, <rs2/imm> 5 1 4 4 4
* Let us use the I bit to specify if the second operand is an * opcode → type of the instruc6on
immediate or a register.
* I bit → 0 (second operand is a register)
* I = 0 → second operand is a register
* dest reg → rd
* I = 1 → second operand is an immediate
* source register 1 → rs1
* Since we have 16 registers, we need 4 bits to specify a register
* source register 2 → rs2
33 34
opcode 1 dest reg src reg1 immediate * cmp, not, and mov
op I rd rs1 imm
2 * Use the 3 address : immediate or register formats
5 1 4 4 modifier bits 18
* Do not use one of the fields
* opcode → type of the instruc6on
* I bit → 1 (second operand is an immediate)
* dest reg → rd
* source register 1 → rs1
* Immediate → imm
* modifier bits → 00 (default), 01 (u), 10 (h)
35 36
9
cmp, not, and mov Load and Store Instructions
32
* ld rd, imm[rs1]
cmp 00101 I rs1 rs2 / imm
32
5 1 4 4 18
ld rd, imm[rs1] 01110 1 rd rs1 imm
32
5 1 4 4 18
not 01000 I rd rs2/ imm
5 1 4 4 18
37 38
5 1 4 4 18
39 40
10
Summary of Instruction Formats Source:
Format Definition
branch op (28-32) offset (1-27)
register op (28-32) I (27) rd (23-26) rs 1(19-22) rs 2(15-18)
immediate op (28-32) I (27) rd (23-26) rs 1(19-22) imm (1-18) Computer Organisation and Architecture
op → opcode, offset → branch offset, I → immediate bit, rd → destination register
rs1 → source register 1, rs2 → source register 2, imm → immediate operand Smruti Ranjan Sarangi,
IIT Delhi
PROPRIETARY MATERIAL. © 2014 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means,
without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. PowerPoint
Slides are being provided only to authorized professors and instructors for use in preparing for classes using the affiliated textbook. No other use or distribution of this PowerPoint slide is permitted.
41 The PowerPoint slide may not be sold and may not be distributed or be used by any student or any other third party. No part of the slide may be reproduced, displayed or distributed in any form or by
any means, electronic or otherwise, without the prior written permission of McGraw Hill Education (India) Private Limited.
Source:
PROPRIETARY MATERIAL. © 2014 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means,
without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. PowerPoint
Slides are being provided only to authorized professors and instructors for use in preparing for classes using the affiliated textbook. No other use or distribution of this PowerPoint slide is permitted.
The PowerPoint slide may not be sold and may not be distributed or be used by any student or any other third party. No part of the slide may be reproduced, displayed or distributed in any form or by
any means, electronic or otherwise, without the prior written permission of McGraw Hill Education (India) Private Limited.
11