Docsity

Execution of Machine Code - Computer Organization and Design - Lecture Slides, Slides of Computer Aided Design (CAD)

Partial preview of the text

Download Execution of Machine Code - Computer Organization and Design - Lecture Slides and more Slides Computer Aided Design (CAD) in PDF only on Docsity!

Execution of Machine Code

Machine State and Operations

Apps

O/S

Arch

mArch

Logic

Digital

Analog

Devices

Physics

Longer Assembly Example

lw 1 0 five load reg1 with 5 (uses symbolic address) lw 2 1 3 load reg2 with -1 (uses numeric address)

start add 1 1 2 decrement reg

beq 0 1 2 goto end of program when reg1 equals 0 beq 0 0 start go back to the beginning of the loop noop

done halt end of program

five .fill 5

neg1 .fill -

stAddr .fill start will contain the address of start (2)

Functions and JALR

Jump and Link Register

• jalr – J-type – opcode 101

• Usage:

jalr RegA RegB

• First store PC+1 into regA, where PC is the

address of the jalr instruction. Then branch to the

address contained in regB.

  • Note : This explicit ordering implies that if regA is the same as regB (i.e. jalr 3 3), the processor will first store PC+1 into that register, then end up branching to PC+1.

Converting high level semantics to assembly

code

C: printf(“hello world\n”);

Need to pass parameters

Need to save return address

Need to jump to printf

Need to get return value (if used)

Execute instructions for printf()

Task 1: Passing parameters

Q: Where should you put all of the parameters?

Registers?

Fast access but few in number and wrong size for some

objects

Memory?

Slower, but good general solution – where in memory?

A: Registers and memory.

Put the first few parameters in registers (if they fit)

Put the rest in memory on the call stack

The MIPS Stack Frame

Saving registers during a call

  • What happens to the values we have in registers when we

make a function call?

  • You can save your registers before you make the function call. - Where?
  • What if the function you are calling doesn’t use that register?

The call frame is used to store anything required to support function calls

We just wasted our time… (and space)

What is the best solution?

  • Advantages to caller saved
  • Advantages to callee saved
  • Hybrid approach

– Have some registers that are caller saved, some

that are callee saved.

  • If you need a register, but not at the site of a call,

allocate a caller saved.

  • If you need a register at a call site, allocate a callee

saved

The programmer (or compiler) should pick the

The MIPS Stack Frame - Updated

Class Problem

• You have:

– 2 caller-save regs

  • (1 and 2)

– 2 callee-save regs

  • (3 and 4)

• What is the best choice of regs for

a, b, c, d?