74
Instructions: Language of The Computer Bo Cheng [email protected]

Instructions: Language of The Computer Bo Cheng [email protected]

  • View
    223

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Instructions: Language of The Computer

Bo Cheng

[email protected]

Page 2: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Compiler - Assembler - Linker - Loader

Compiler: transforms a C program into an assembly language program, a symbolic form of what the machine understand.

Assembler: turns the assembly language program into an object file, which is a combination of machine language instructions, data and information needed to place instructions properly in memory.

Linker or link editor: takes all the independently assembled machine language programs and ‘stitches’ them together into an executable file that can be run on a computer.

– The linker uses the relocation information and symbol table in each object module to resolve all undefined labels.

Loader: load the executable file into memory for execution.

Page 3: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Other Information

Symbol table: A table that matches names of labels to the addresses of the memory words that instructions occupy

Executable file: A functional program in the format of an object file that contains no unresolved references, relocation information, symbol table, or debugging information.

Page 4: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Library: Static vs. Dynamic

Static:– Fast– Becoming a part of the executable code– Loading the whole library no matter that is running

or not

Dynamic:– Not linked and loaded until the program is run– Pay a good deal of overhead the first time a

routine is called.

Page 5: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

A Translation Hierarchy For C

C Program

Assembly Language Program

Object: Machine Language Module Object: Binary Routine (Machine Language)

Executable: Machine Language Program

Memory

Compiler

Assembler

Linker

Loader

X.cX.c

X.sX.asm

X.oX.obj

Y.aY.lib

staticY.soY.dll

dynamic

a.outX.exe

Page 6: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Language, Language, Language

Page 7: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Machine language

Computer instructions can be represented as sequences of bits.

This is the lowest possible level of representation for a program

Can be understood directly by the machine. Each instruction is equivalent to a single,

indivisible action of the CPU.

Page 8: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Assembly Language

A slightly higher-level representation (and one that is much easier for humans to use)

Very closely related to machine language Assembler

– Translate programs written in assembly language into machine language.

Because of the close relationship between machine and assembly languages, each different machine architecture usually has its own assembly language

– Sometimes, each architecture may have several

Page 9: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

MIPS

Microprocessor without interlocked pipeline stages A RISC microprocessor architecture developed by M

IPS Computer Systems Inc. MIPS designs are used in SGI's computer product lin

e, and have found broad application in embedded systems, Windows CE devices, and Cisco routers.

The Nintendo 64 console, Sony PlayStation 2 console, and Sony PSP handheld system use MIPS processors.

http://en.wikipedia.org/wiki/MIPS_architecture

Page 10: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

MIPS History (I)

In 1981, a team led by John Hennessy at Stanford University started work on what would become the first MIPS processor.

The basic concept was to dramatically increase performance through the use of deep instruction pipelines

Generally a pipeline spreads out the task of running an instruction into several steps, and then start working on "step one" of an instruction even before the preceding instruction is complete.

Page 11: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

MIPS History (II)

In 1984, Hennessy left Stanford to form MIPS Computer Systems.

They released their first design, the R2000, in 1985, improving the design as the R3000 in 1988.

These 32-bit CPUs formed the basis of their company through the 1980s, used primarily in SGI's series of workstations.

In 1991 MIPS released the first 64-bit microprocessor, the R4000.

SGI bought the company outright in 1992 – Becoming an internal group at SGI, the company was now

known as MIPS Technologies.

Page 12: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

MIPS History (III)

By the late 1990s MIPS was a powerhouse in the embedded processor field, and in 1997 the 48th million MIPS-based CPU shipped

– The first RISC CPU to outship the famous Motorola 68000 family (CISC).

This proved fairly successful due to the simplicity of the core

– much less capable CISC designs of similar gate count and price

– the two are strongly related, the price of a CPU is generally the number of gates plus the number of external pins.

Page 13: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

MIPS R2000 CPU and FPU

A MIPS processor consists of – an integer processing unit (the

CPU) and – a collection of coprocessors

that perform ancillary tasks or operate on other types of data such as floating point numbers

SPIM simulates two coprocessors

– Coprocessor 0 handles traps, exceptions, and

the virtual memory system. – Coprocessor 1

floating point unit.

Page 14: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Central Processing Unit - CPU

the brains of the computer Sometimes referred to simply as the processor or central

processor On large machines, CPUs require one or more printed circuit

boards. On personal computers and small workstations, the CPU is

housed in a single chip called a microprocessor. Two typical components of a CPU are:

– The arithmetic logic unit (ALU), which performs arithmetic and logical operations.

– The control unit, which extracts instructions from memory and decodes and executes them, calling on the ALU when necessary.

http://www.webopedia.com/TERM/C/CPU.html

Page 15: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

SPIM

A software simulator that runs programs written for MIPS R2000/R3000 processors.

SPIM is MIPS backwards PC-SPIM is the windows version of SPIM SPIM can read and immediately execute assembly la

nguage files, but not binary. Contains a debugger and provides a few operating s

ystem-like services. It is much slower than real computer (100 or more ti

mes) Can be downloaded from http://

www.cs.wisc.edu/~larus/spim.html

Page 16: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Use PC-SPIM

Page 17: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

PCSpim Windows Interface

Registers window– shows the values of all registers in

the MIPS CPU and FPU Text segment window

– shows assembly instructions & corresponding machine code

Data segment window– shows the data loaded into the

program’s memory and the data of the program’s stack

Messages window– shows PCSpim messages

Separate console window appears for I/O

Source: http://www.cs.ait.ac.th/~guha/COA/Spim/spimSlides.ppt

Page 18: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Opening Window

Register Display: This shows the contents (bit patterns in hex) of all 32 general purpose registers, the floating point registers, and a few others.

Text Display: This shows the assembly language program source, the machine instructions (bit patterns in hex) they correspond to, and the addresses of their memory locations.

Data and Stack Display: This shows the sections of MIPS memory that hold ordinary data and data which has been pushed onto a stack.

SPIM Messages: This shows messages from the simulator (often error messages).

Character output from the simulated computer is in the SPIM console window Character output from the simulated computer is in the SPIM console window

Page 19: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Setting ….

Page 20: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Message

Messages from the simulated computer appear in the console window when an assembly program that is running (in simulation) writes to the (simulated) monitor. If a real MIPS computer were running you would see the same messages on a real monitor.

Page 21: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Writing an Assembly Program

A source file (in assembly language or in any programming language) is the text file containing programming language statements created (usually) by a human programmer.

An editor like Notepad will work. You will probably want to use a better editor.

Word processors usually create "binary" files and so are not suitable for creating source files.

With your program (text) editor create a file with asm extension, e.g., addup.asm.

Page 22: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Use Notepad To Edit Your Program

Page 23: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Program Template

# Comment giving name of program and description of function# Template.s# Bare-bones outline of MIPS assembly language program

.data # variable declarations follow this line # ...

.text # instructions follow this line

main: # indicates start of code (first instruction to execute) # ...

# End of program, leave a blank line afterwards to make SPIM happy

Page 24: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Two Sections

Text– Instructions go here– Contains the beginning of the program

Data– Where the variables are declared

Page 25: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Example 1

# Daniel J. Ellard -- 02/21/94# add.asm-- A program that computes the sum of 1 and 2,# leaving the result in register $t0.# Registers used:# t0 - used to hold the result.# t1 - used to hold the constant 1.# v0 - syscall parameter.main: # SPIM starts execution at main.

li $t1, 1 # load 1 into $t1.add $t0, $t1, 2 # compute the sum of $t1 and 2, and

# put it into $t0.li $v0, 10 # syscall code 10 is for exit.syscall # make the syscall.

# end of add.asm

Page 26: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Comments

Any text between a pound sign (#) and the subsequent newline is considered to be a comment.

Comments are absolutely essential! Assembly language programs are notoriously

difficult to read unless they are properly documented.

Page 27: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Labels and Main

To begin with, we need to tell the assembler where the program starts.– In SPIM, program execution begins at the location

with the label main. A label is a symbolic name for an address in

memory. – a label is a symbol name followed by a colon– e.g., main: – The names of instructions can not be used as

labels

Page 28: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Registers

The MIPS R2000 CPU has 32 registers. 31 of these are general-purpose registers

that can be used in any of the instructions. The last one, denoted register zero, is

defined to contain the number zero at all times.

MIPS programmers have agreed upon a set of guidelines that specify how each of the registers should be used.

Page 29: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The MIPS Register Set (32 Registers)

Page 30: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The MIPS Instruction Set (I)

If an instruction description begins with an , o then the instruction is not a member of the native MIPS instruction set– For example, abs– The assembler translates pseudoinstructions into

one or more native instructions

Page 31: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The MIPS Instruction Set (II)

If the op contains a (u), then this instruction can either use signed or unsigned arithmetic, depending on whether or not a u is appended to the name of the instruction.

For example, if the op is given as add(u)– add (add signed) or – addu (add unsigned).

Page 32: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The MIPS Instruction Set (III)

Page 33: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The MIPS Instruction Set (IV)

des must always be a register. src1 must always be a register. reg2 must always be a register. src2 may be either a register or a 32-bit integ

er. addr must be an address

Page 34: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The Load Instructions

• Fetch a byte, halfword, or word from memory and put it into a register. • The li and lui instructions load a constant into a register.

Page 35: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Arithmetic Instructions

Page 36: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Arithmetic Examples

<op> <des> <src1> <src2> Have 3 operands Operand order is fixed: destination first Only 32 registers are provided Examples

– add $t0, $s0, $s2 # $t0 = $s0 + $s2– sub $s0, $t0, $t1 # $s0 = $t0 – $t1

Page 37: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Syscalls (I)

Page 38: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Syscalls (II)

The syscall instruction suspends the execution of your program and transfers control to the operating system.

The operating system then looks at the contents of register $v0 to determine what it is that your program is asking it to do.

For example: Similar to C, where the exit function can be called in order to halt the execution of a program

Page 39: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Syscalls (III) - Example

syscall 5 can be used to read an integer into register $v0.

syscall 1 can be used to print out the integer stored in $a0.

Page 40: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Data Movement Instructions

•The data movement instructions move data among registers. •Special instructions are provided to move data in and out of special registers such as hi and lo.

Page 41: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Example 2 (I)

# Daniel J. Ellard -- 02/21/94# add2.asm-- A program that computes and prints the sum# of two numbers specified at runtime by the user.# Registers used:# $t0 - used to hold the first number.# $t1 - used to hold the second number.# $t2 - used to hold the sum of the $t1 and $t2.# $v0 - syscall parameter and return value.# $a0 - syscall parameter.

Page 42: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Example 2 (II)main:## Get first number from user, put into $t0.

li $v0, 5 # load syscall read_int into $v0.syscall # make the syscall.move $t0, $v0 # move the number read into $t0.

## Get second number from user, put into $t1.li $v0, 5 # load syscall read_int into $v0.syscall # make the syscall.move $t1, $v0 # move the number read into $t1.

# Compute the sum.add $t2, $t0, $t1 # Sum it up

## Print out $t2.move $a0, $t2 # move the number to print into $a0.li $v0, 1 # load syscall print_int into $v0.syscall # make the syscall.

# Exit the program li $v0, 10 # syscall code 10 is for exit.

syscall # make the syscall.# end of add2.asm.

Page 43: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Example 3 – Hello World

# Daniel J. Ellard -- 02/21/94# hello.asm-- A "Hello World" program.# Registers used:# $v0 - syscall parameter and return value.# $a0 - syscall parameter-- the string to print.

.textmain:

la $a0, hello_msg # load the addr of hello_msg into $a0.li $v0, 4 # 4 is the print_string syscall.syscall # do the syscall.

# Exit the programli $v0, 10 # 10 is the exit syscall.syscall # do the syscall.

# Data for the program:.datahello_msg: .asciiz "Hello World\n"

# end hello.asm

Page 44: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Directives

A directive is an instruction for the assembler (not the CPU) for reserving memory, telling the assembler where to place instructions, etc.

Data segment– Tagged with the .data directive. – Is used to allocate storage and initialize global variables

Text segment– Indicated by the .text directive. – This is where we put the instructions we want the processor

to execute. By default, the assembler starts in the text segment

Page 45: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Data Directives

Page 46: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

They Are The Same

Page 47: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Example 4 (I) – Larger Number# Daniel J. Ellard -- 02/21/94# larger.asm-- prints the larger of two numbers specified# at runtime by the user.# Registers used:# $t0 - used to hold the first number.# $t1 - used to hold the second number.# $t2 - used to store the larger of $t1 and $t2.# $v0 - syscall parameter and return value.# $a0 - syscall parameter.

.textmain:## Get first number from user, put into $t0.

li $v0, 5 # load syscall read_int into $v0.syscall # make the syscall.move $t0, $v0 # move the number read into $t0.

## Get second number from user, put into $t1.li $v0, 5 # load syscall read_int into $v0.syscall # make the syscall.move $t1, $v0 # move the number read into $t1.

Page 48: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Example 4 (II) – Larger Number

## put the larger of $t0 and $t1 into $t2.bgt $t0, $t1, t0_bigger # If $t0 > $t1, branch to t0_bigger,move $t2, $t1 # otherwise, copy $t1 into $t2.b endif # and then branch to endif

t0_bigger:move $t2, $t0 # copy $t0 into $t2

endif:## Print out $t2.

move $a0, $t2 # move the number to print into $a0.li $v0, 1 # load syscall print_int into $v0.syscall # make the syscall.

## exit the program.li $v0, 10 # syscall code 10 is for exit.syscall # make the syscall.

# end of larger.asm.

Page 49: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Branch Instructions

Page 50: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Bgt and b statement

<bgt> <Src1> <Src2><Label> The rst two are numbers, and the last is a lab

el. If (Src1 > Src2) Go to <Label>; otherwise go

next

<b> <Label> Simply branches to the given label.

Page 51: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Computing Integer DivisionIterative C++ Version

int a = 12;int b = 4;int result = 0;main () { while (a >= b) { a = a - b; result ++; } }}

MIPS/SPIM Version

MIPS Assembly Language

C++

.data # Use HLL program as a comment

x: .word 12 # int x = 12;

y: .word 4 # int y = 4;

res: .word 0 # int res = 0;

.globl main

.text

main: la $s0, x # Allocate registers for globals

lw $s1, 0($s0) # x in $s1

lw $s2, 4($s0) # y in $s2

lw $s3, 8($s0) # res in $s3

while: bgt $s2, $s1, endwhile # while (x >= y) {

sub $s1, $s1, $s2 # x = x - y;

addi $s3, $s3, 1 # res ++;

j while # }

endwhile:

la $s0, x # Update variables in memory

sw $s1, 0($s0)

sw $s2, 4($s0)

sw $s3, 8($s0)

Page 52: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Simple One

int a = 12;int b = 4;int result = 0;main () { while (a >= b) { a = a - b; result ++; } printf(“%d %d %d, a , b, res); }

MIPS Assembly Language

C++

# $t0 = a# $t1 = b# $t2 = res

.textmain:

li $t0, 12li $t1, 4li $t2, 0

while: bgt $t1, $t0, endwhile # while (a >= b) {sub $t0, $t0, $t1 # a = a - b; addi $t2, $t2, 1 # res ++; j while # }

endwhile:move $a0, $t0li $v0, 1 syscall # make the syscall.move $a0, $t1li $v0, 1 syscall # make the syscall.move $a0, $t2li $v0, 1 syscall # make the syscall.

#li $v0, 10 # syscall code 10 is for exit.syscall # make the syscall.

Page 53: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Jump Instructions

Page 54: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Comparison Instructions

Page 55: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The Address Mode

The second operand of all of the load and store instructions must be an address. The

MIPS architecture supports the following addressing modes:

Page 56: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Subroutine

Sometimes called procedure, function, or method

Is a logical division of the code that may be regarded as a self-contained operation.

A subroutine might be executed several times with different data as the program executes.

http://chortle.ccsu.edu/AssemblyTutorial/TutorialContents.html

Chap 26 & 27

Page 57: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Callers and Callees

A subroutine call is when a main routine (or other routine) passes control to a subroutine.

The main routine is said to be the CALLER and the subroutine is said to be the CALLEE.

A return from a subroutine is when a subroutine passes control back to its CALLER.

Page 58: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The jal Instruction (I)

The jal instruction and register $31 ($ra) provide the hardware support necessary to elegantly implement subroutines.

Machine Cycle

Page 59: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The jal Instruction (II)

So now $ra holds the address of the second instruction after the jal instruction.

jal sub # $ra <― PC+4 # $ra <― address 8 bytes away from the jal # PC <― sub # load the PC with the subroutine entry point

jal sub # $ra <― PC+4 # $ra <― address 8 bytes away from the jal # PC <― sub # load the PC with the subroutine entry point

Page 60: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The jr Instruction

Returns control to the caller.

jr $ra # PC <― $ra It copies the contents of $ra into the PC: Think as "jumping to the address in $ra." The jr instruction is followed by a branch dela

y slot (nop instruction).

Page 61: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Calling Convention

A subroutine is called using jal. The subroutine returns to its caller using jr $ra. Registers are used as follows:

– $t0 - $t9 — The subroutine is free to change these registers. – $s0 - $s7 — The subroutine must not change these registers. – $a0 - $a3 — These registers contain arguments for the subro

utine. The subroutine can change them. – $v0 - $v1 — These registers contain values returned from the

subroutine. The main routine returns control by using the exit servi

ce (service 10) of the SPIM exception handler.

Page 62: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Main Calling Mysub Example

Two arguments are passed, in $a0 and $a1.

The subroutine reads the arguments from those registers.

Page 63: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Example 5# Bo Cheng -- 02/08/05# ex5.asm-- A program that exercises the function calls

.datain_main_msg1: .asciiz "Before The Call \n"in_sub_msg: .asciiz "In sub Program \n"in_main_msg2: .asciiz "After The Call \n".text

main: # SPIM starts execution at main.# Print the "before" message

la $a0, in_main_msg1li $v0, 4 syscall

# Call the subroutine sub_pro jal sub_pronop

# Print "after" message la $a0, in_main_msg2li $v0, 4 syscall

# exit the programli $v0, 10 # syscall code 10 is for exit.syscall # make the syscall.

# end of add.asm

# the subrouine body sub_pro:

la $a0, in_sub_msgli $v0, 4 syscall

# return the calljr $ranop

Page 64: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The Example 6 - Sum

main: li $s0, 0x06 # load 6 into Register S0li $s1, 0x10 # load 16 into Register S1move $a0, $s0 # use argument 1 in Register a0move $a1, $s1 # use argument 2 in Register a1jal sum_it # call subroutine sum_itnop # branch delay slot

# Get the resultmove $s3, $v0 # get the result from Register v0

# Print the summove $a0, $s3 # place the result into Register a0li $v0, 1 # load syscall print_int into $v0.syscall # make the syscall.

# exit the programli $v0, 10 # syscall code 10 is for exit.syscall # make the syscall.

# end of sum_example.asm

# the subroutine sum_it sum_it:

add $t1, $a0, $a1 # sum it upmove $v0, $t1 # place the result jr $ra # returnnop # branch delay slot

Page 65: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Pushing the Return Address

To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed.

But this address does not have to remain in $ra all the time the subroutine is running.

It works fine to save the value of $ra and then to restore it when needed.

Page 66: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Only one $ra would be lost if nested subroutine– Solution: push the return addr

ess it gets onto the stack. When it returns to its caller, it pops the stack to get the return address.

Need to change registers in subroutine– Solution: push the contents o

nto stack

Chain of Subroutine Calls

Page 67: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

LIFO (Last-In, Fist-Out) Grows from larger memory addr

esses to smaller memory addresses

Use stack pointer ($SP=$29) to point the top of stack.

Push: SP = SP – 4

Pop: SP = SP + 4

Stack

0x010000000x00FFFFFC

0x00FFFFF40x00FFFFF8

0x00FFFFF0

SPsub $sp, 4 sw $ra, ($sp)

lw $ra, ($sp) add $sp, 4

Page 68: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Push on MIPS

Source: users.ece.gatech.edu/~rdanse/ ECE2030/slides/ECE2030_Chapter15_2pp.pdf

Page 69: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Pop On MIPS

Page 70: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Nested Procedure Calls

Page 71: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Stack-based Linkage Convention

Subroutine Call (done by the caller): – Push onto the stack any registers $t0-$t9 that contain values that must be saved.– Put argument values into $a0-$a3. – Call the subroutine using jal.

Subroutine Prolog (done by the subroutine at its beginning): – If this subroutine might call other subroutines, push $ra onto the stack. – Push onto the stack any registers $s0-$s7 that this subroutine might alter.

Subroutine Body: – The subroutine may alter any "T" or "A" register, or any "S" register– If the subroutine calls another subroutine, then it does so by following these rules.

Subroutine Epilog (done by the subroutine just before it returns to the caller): – Put returned values in $v0-$v1 – Pop from the stack (in reverse order) any registers $s0-$s7 that were pushed in the p

rolog (step 5). – If it was pushed in the prolog (step 4), pop the return address from the stack into $ra. – Return to the caller using jr $ra.

Regaining Control from a subroutine (done by the caller): – Pop from the stack (in reverse order) any registers $t0-$t9 that were previously pushe

d (step 1).

Page 72: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Pushing and Popping Registers

if a subroutine is expected to alter any of the "S" registers, it must first push their values onto the stack.

Just before returning to the caller it must pop these values from the stack back into the registers they came from.

Page 73: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

The Call Chain ExamplesubB: sub $sp,$sp,4 # push $ra sw $ra,($sp) . . . . jal subC # call subC nop . . . . lw $ra,($sp) # pop return address add $sp,$sp,4 jr $ra # return to caller nop

# subC expects to use $s0 and $s1 # subC does not call another subroutine# subC: sub $sp,$sp,4 # push $s0 sw $s0,($sp) sub $sp,$sp,4 # push $s1 sw $s1,($sp) . . . . # statements using $s0 and $s1 lw $s1,($sp) # pop s1 add $sp,$sp,4 lw $s0,($sp) # pop s0 add $sp,$sp,4 jr $ra # return to subB nop

Page 74: Instructions: Language of The Computer Bo Cheng bchneg@ccu.edu.tw

Example 7 – Find Minmain:

li $a0, 3 # set arg 0 li $a1, 4 # set arg 1 li $a2, 5 # set arg 2 jal findMin3 move $t0, $v0 # save return value to $t0

## Print out the min.move $a0, $t0 # move the number to print into $a0.li $v0, 1 # load syscall print_int into $v0.syscall # make the syscall.

# exit the programli $v0, 10 # syscall code 10 is for exit.syscall # make the syscall.

# end of add.asm

findMin3: move $t0, $a0 # min = x bge $a1, $t0, IF2 # branch if !( y < min ) move $t0, $a1 # min = yIF2: bge $a1, $t0, END # branch if !( z < min ) move $t0, $a2 # min = zEND: move $v0, $t0 # retval = min jr $ra # return