1 abstractions and computers and the mal programming language

60
1 Abstractions and Computers and the MAL programming Language

Post on 21-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Abstractions and Computers and the MAL programming Language

1

Abstractions and Computers and the MAL programming

Language

Page 2: 1 Abstractions and Computers and the MAL programming Language

2CMPE12c Gabriel Hugh Elkaim

Computer Architecture

Definition: Interface between a computers hardware and its software. Defines exactly what the computer’s instructions do, and how they are specified.

Instruction Set Architecture (ISA)

Page 3: 1 Abstractions and Computers and the MAL programming Language

3CMPE12c Gabriel Hugh Elkaim

MIPSmachine language

TALMALSAL

• SAL – Simple Abstract Language• MAL – MIPS Assembly Language• TAL – True Assemble Language

Computer Architecture

Page 4: 1 Abstractions and Computers and the MAL programming Language

4CMPE12c Gabriel Hugh Elkaim

HighLevel

Language

AssemblyLanguage

MachineLanguage

Compiler Assembler

Compiler: A computer program that translates code written in a high level language into an intermediate level abstract language.

Computer Architecture

Page 5: 1 Abstractions and Computers and the MAL programming Language

5CMPE12c Gabriel Hugh Elkaim

Computer Science

Definition: Fundamentally the study of algorithms and data structures.

Abstraction: Use of level of abstraction in software design allows the programmer to focus on a critical set of problems without having to deal with irrelevant details.

Page 6: 1 Abstractions and Computers and the MAL programming Language

6CMPE12c Gabriel Hugh Elkaim

Procedure or Function

int average (a, b)begin

avg = (a+b)/2;

return (avg);end

main ()…x = 4;y = 2;k = average (x,y);printf (“%d”, k);…

Computer Science

Page 7: 1 Abstractions and Computers and the MAL programming Language

7CMPE12c Gabriel Hugh Elkaim

CPU(MIPS)

Computer

MemoryWrite data

Read data

Control info

CPU Interacts with the memory in 3 ways:• fetches instructions• loads the value of a variable• stores the new value of a variable

Memory is capable of only 2 operations:• reads – a load or a fetch• writes – operation of a storing the value of a variable

Page 8: 1 Abstractions and Computers and the MAL programming Language

8CMPE12c Gabriel Hugh Elkaim

Page 9: 1 Abstractions and Computers and the MAL programming Language

9CMPE12c Gabriel Hugh Elkaim

Instruction Fetch / Execute Cycle

In addition to input & output a program also:

•Evaluates arithmetic & logical functions to determine values to assign to variable.•Determines the order of execution of the statements in the program.

In assembly this distinction is captured in the notion of Arithmetic, logical, and control instructions.

Page 10: 1 Abstractions and Computers and the MAL programming Language

10CMPE12c Gabriel Hugh Elkaim

Arithmetic and logical instructions evaluate variables and assign new values to variables.

Control instructions test or compare values of a variable and makes decisions about what instruction is to be executed next.

Program Counter (PC)Basically the address at which the current executing instruction exists.

Instruction Fetch / Execute Cycle

Page 11: 1 Abstractions and Computers and the MAL programming Language

11CMPE12c Gabriel Hugh Elkaim

1. load rega, 102. load regb, 203. add regc, rega, regb4. beq regc, regd, 85. store regd, rege6. store regc, regd7. load regb, 158. load rega, 30

PC

Instruction Fetch / Execute Cycle

Address

Page 12: 1 Abstractions and Computers and the MAL programming Language

12CMPE12c Gabriel Hugh Elkaim

The CPU begins the execution of an instruction by supplying the value of the PC to the memory & initiating a read operation (fetch).

The CPU “decodes” the instruction by identifying the opcode and the operands.

PC increments automatically unless a control instruction is used.

Instruction Fetch / Execute Cycle

Page 13: 1 Abstractions and Computers and the MAL programming Language

13CMPE12c Gabriel Hugh Elkaim

• CPU Fetches Instruction• Decodes it and sees it is an add

operation, needs to get values for the variables “B” & “C”

• CPU executes a load operation, gives address of variable “B”

• Does the same for variable “C”• Does the “add” operation and stores the

result in location of variable “A”

Instruction Fetch / Execute Cycle

For example:

PC add A, B, C

Page 14: 1 Abstractions and Computers and the MAL programming Language

14CMPE12c Gabriel Hugh Elkaim

Branch – like a goto instruction, next instruction to be fetched & executed is an instruction other than the next in memory.

Instruction Fetch / Execute Cycle

add A, B, Cbeq A, 5, fredsub A, D, 3

fred: sub A, D, 4

If A==5 then next instruction is at fred

Page 15: 1 Abstractions and Computers and the MAL programming Language

15CMPE12c Gabriel Hugh Elkaim

Breaking down an instruction

add a, b, c

a b cadd

Opcode

Destination register

Source registers

Page 16: 1 Abstractions and Computers and the MAL programming Language

16CMPE12c Gabriel Hugh Elkaim

Locality of reference

We need techniques to reduce the instruction size. From observation of programs we see that a small and predictable set of variables tend to be referenced much more often than other variables.

Page 17: 1 Abstractions and Computers and the MAL programming Language

17CMPE12c Gabriel Hugh Elkaim

Basically, locality is an indication that memory is not referenced randomly.

This is where the use of registers comes into play.

Page 18: 1 Abstractions and Computers and the MAL programming Language

18CMPE12c Gabriel Hugh Elkaim

Registers and MAL

ALURegister

ArrayMemory CtrlData cacheInst. cache

IO

Memory(disk)

Program “code” is in memory (or cache), use registers to hold commonly used variables for faster execution

Page 19: 1 Abstractions and Computers and the MAL programming Language

19CMPE12c Gabriel Hugh Elkaim

CISC vs. RISC

CISC : complex instruction set computerLots of instructions of variable size, very memory optimal, typically less registers.

RISC : reduced instruction set computerLess instructions all of a fixed size, more registers, optimized for speed. Usually a “Load/Store” architecture.

Page 20: 1 Abstractions and Computers and the MAL programming Language

20CMPE12c Gabriel Hugh Elkaim

Specifying addresses

For a load/store architecture, registers are used to supply source operands and receive results from all instructions except loads and stores.

Basically, load the registers with the operands from memory first, then perform the operation.

Page 21: 1 Abstractions and Computers and the MAL programming Language

21CMPE12c Gabriel Hugh Elkaim

How do we fit the “stuff” in 32-bit instructions?

So we have arithmetic instructions and branch type instructions that cannot contain all the needed info in a single 32-bit word.

Page 22: 1 Abstractions and Computers and the MAL programming Language

22CMPE12c Gabriel Hugh Elkaim

opcode addressregreg Effective

address

2. Instruction might specify a register that contains the address, 1 word instruction.

1. Instruction might occupy 2 words.

opcode addressregEffectiveaddress

Ways to get an effective address

Page 23: 1 Abstractions and Computers and the MAL programming Language

23CMPE12c Gabriel Hugh Elkaim

3. Instruction might specify a small constant and a second register, 1 word instruction.

opcode reg constant

addressreg + Effective address

Effective Address Calculation

Page 24: 1 Abstractions and Computers and the MAL programming Language

24CMPE12c Gabriel Hugh Elkaim

4. The instruction might specify 2 additional registers, 1 word instruction.

opcode reg reg

addressreg addressreg

+

Effective address

Effective Address Calculation

Page 25: 1 Abstractions and Computers and the MAL programming Language

25CMPE12c Gabriel Hugh Elkaim

Addressing modes

Methods a computer uses to specify an address within an instruction.

Page 26: 1 Abstractions and Computers and the MAL programming Language

26CMPE12c Gabriel Hugh Elkaim

• Immediate– The operand is contained directly in

the instruction. Ex: li reg1, 5

• Register– The operand is contained in a

register. Ex: add reg1, reg2, reg3

• Direct– The address of the operand is

contained in the instruction (two-word instruction)

Addressing Modes

Page 27: 1 Abstractions and Computers and the MAL programming Language

27CMPE12c Gabriel Hugh Elkaim

• Register Direct– The address of the operand is

contained in a register. Ex: lw reg1, reg3

• Base Displacement– The address is computed as the sum of

the contents of a register (the base) and a constant contained in the instruction (the displacement). Ex: lw reg1, 5(reg3)

Addressing Modes

Page 28: 1 Abstractions and Computers and the MAL programming Language

28CMPE12c Gabriel Hugh Elkaim

• IndirectThe instruction specifies a register

containing an address the content of which is the address of the operand

opcode reg

address

address

reg

Effective address

Memoryaddress

Addressing Modes

Page 29: 1 Abstractions and Computers and the MAL programming Language

29CMPE12c Gabriel Hugh Elkaim

MAL

2 distinct register files:

• 32 general registers• 16 floating point registers

(MIPS Assembly Language)

MIPS is a load/store RISC architecture

Page 30: 1 Abstractions and Computers and the MAL programming Language

30CMPE12c Gabriel Hugh Elkaim

The 32 general registers are numbered $0 - $31.

$0 is always the value “Zero”.

$1 is used by the assembler.

$26 & $27 are used by the operating system.

$28, $29, & $31 have special conventions for the use of them.

MAL

Page 31: 1 Abstractions and Computers and the MAL programming Language

31CMPE12c Gabriel Hugh Elkaim

Common aliases for registers

$2-$3 $v0-$v1 procedure results$4-$7 $a0-$a3 parameters for

procedure$8-$15 $t0-$t7 temporary registers$24-$25 $t8-$t9$16-$23 $s0-$s7 saved registers$30 $s8$29 $sp stack pointer$31 $ra return address register

Page 32: 1 Abstractions and Computers and the MAL programming Language

32CMPE12c Gabriel Hugh Elkaim

The 16 floating point registers are intended exclusively for holding floating point operands. These registers are 64-bits in size for holding both single precision (32-bit) floats and double precision (64-bit) floats.

These registers are named $f0, $f2, $f4,…., $f30.

Why?

MAL

Page 33: 1 Abstractions and Computers and the MAL programming Language

33CMPE12c Gabriel Hugh Elkaim

MAL uses a single, versatile addressing mode for its regular load store.

Base displacement.

General since its special cases provide for both direct and register direct address.

MAL

Page 34: 1 Abstractions and Computers and the MAL programming Language

34CMPE12c Gabriel Hugh Elkaim

MAL has 3 basic types:

• Integer• Floating point• Character

Syntax of MAL

one instruction, declaration per linecomments are anything on a line following #comments may not span lines

MAL Syntax

Page 35: 1 Abstractions and Computers and the MAL programming Language

35CMPE12c Gabriel Hugh Elkaim

“C”type variablename;

“MAL”variablename: type value

type is.word (integer).byte (character).float (floating point)

value is optional – the initial value

MAL Syntax

Page 36: 1 Abstractions and Computers and the MAL programming Language

36CMPE12c Gabriel Hugh Elkaim

Examples:flag: .word 0counter: .word 0variable3: .worde: .float 2.71828uservalue: .byteletter: .byte ‘a’

•One declaration per line•Default initial value is 0(but you may lose points if you depend on this)

MAL Syntax

Page 37: 1 Abstractions and Computers and the MAL programming Language

37CMPE12c Gabriel Hugh Elkaim

Directives give information to the assembler. All directives start with ‘.’ (period)

Examples:.byte.word.floatmain:

# tells simulator to start execution at this location..data

# .data identifies the start of the declaration section # there can be more than 1 .data sections in a program.

MAL Syntax

Page 38: 1 Abstractions and Computers and the MAL programming Language

38CMPE12c Gabriel Hugh Elkaim

.text# identifies where instructions are, there can

be # more than 1 .text sections in a program

.asciiz “a string.\n” # places a string into memory and null

terminates# the string

.ascii “new string.”# places a string into memory with no null# termination.

MAL Syntax

Page 39: 1 Abstractions and Computers and the MAL programming Language

39CMPE12c Gabriel Hugh Elkaim

MAL lw $s1, x lw $s2, y move $s3, $s2 add $s3, $s1, $s2 sub $s3, $s1, $s2 mul $s3, $s1, $s2 div $s3, $s1, $s2 rem $s3, $s1, $s2 sw $s3, z

C

z = y; z = x + y; z = x - y; z = x * y; z = x / y;

z = x % y;

An immediate is a value specified in an instruction, not in the .data section.Examples: li $s2, 0 # load immediate

add $s2, $s2, 3 # add immediate

MAL Syntax

Page 40: 1 Abstractions and Computers and the MAL programming Language

40CMPE12c Gabriel Hugh Elkaim

Simple MAL program

.data avg: .word 0 i1: .word 20 i2: .word 13 i3: .word 2 .textmain:

lw $s1, i1 lw $s2, i2 lw $s3, i3 add $s4, $s1, $s2

div $s4, $s4, $s3 sw $s4, avg li $2, 10 # done cmd syscall

MAL Program

Page 41: 1 Abstractions and Computers and the MAL programming Language

41CMPE12c Gabriel Hugh Elkaim

• Assembler translates to executable– machine language

• Linker combines multiple MAL files• Loader puts executable into

memory and makes the CPU jump to the first instruction “main:”– Executes– When done returns to OS

• Simulator or Monitor

• To rerun with different data, repeat the process

Program Execution

Page 42: 1 Abstractions and Computers and the MAL programming Language

42CMPE12c Gabriel Hugh Elkaim

HLL – if/else statements…

if (condition) statement;

else statement;

“C” if (count < 0) count = count + 1;

MAL Programming

Page 43: 1 Abstractions and Computers and the MAL programming Language

43CMPE12c Gabriel Hugh Elkaim

“MAL” lw $t1, countbltz $t1, ifstuffb endif

ifstuff: add $t1, $t1, 1 endif: # next instruction goes here

“OR” lw $t1, countbgez $t1, endifadd $t1, $t1, 1

endif: # next instruction goes here

MAL Programming

Page 44: 1 Abstractions and Computers and the MAL programming Language

44CMPE12c Gabriel Hugh Elkaim

Loops can be built out of IF’s – WHILE:

“C” while (count > 0)

{a = a % count;

count--;}

MAL Programming

Page 45: 1 Abstractions and Computers and the MAL programming Language

45CMPE12c Gabriel Hugh Elkaim

“MAL”

lw $s1, countlw $s2, a

while: blez $s1, endwhilerem $s2, $s2, $s1sub $s1, $s1, 1b while

endwhile: sw $s2, asw $s1, count

MAL Programming

Page 46: 1 Abstractions and Computers and the MAL programming Language

46CMPE12c Gabriel Hugh Elkaim

Repeat loops

“C”/* do statement while expression is TRUE *//* when expression is FALSE, exit loop */do {

if (a < b)a++;

if (a > b)a--;

} while (a != b)

MAL Programming

Page 47: 1 Abstractions and Computers and the MAL programming Language

47CMPE12c Gabriel Hugh Elkaim

“MAL”lw $s3, alw $s4, b

repeat: bge $s3, $s4, secondifadd $s3, $s3, 1

secondif: ble $s3, $s4, untilsub $s3, $s3, 1

until: bne $s3, $s4, repeat

MAL Programming

Page 48: 1 Abstractions and Computers and the MAL programming Language

48CMPE12c Gabriel Hugh Elkaim

While Loops (Part II)

“C”while ( (count < limit) && (c ==d) )

{ /* loop’s code goes here */}“MAL”

while: ??

# loop code goes here ?

endwhile:

MAL Programming

Page 49: 1 Abstractions and Computers and the MAL programming Language

49CMPE12c Gabriel Hugh Elkaim

Page 50: 1 Abstractions and Computers and the MAL programming Language

50CMPE12c Gabriel Hugh Elkaim

For loops

“C”for ( I = 3; I <= 8; I++)

{ a = a+I;}“MAL”

?for: ?

???

endfor:

MAL Programming

Page 51: 1 Abstractions and Computers and the MAL programming Language

51CMPE12c Gabriel Hugh Elkaim

Page 52: 1 Abstractions and Computers and the MAL programming Language

52CMPE12c Gabriel Hugh Elkaim

Procedure Calls

Simple procedure calls require 2 instructions:

“JR” Jump Register• Be careful with registers!!• Cannot nest unless $ra is saved elsewhere• Cannot be recursive without a stack

“JAL” Jump and Link•Link means save the return address in $ra ($31)

Page 53: 1 Abstractions and Computers and the MAL programming Language

53CMPE12c Gabriel Hugh Elkaim

MAL Procedures

jal average # calls proc.

average: add $s2, $s3, $s4div $s2, $s2, 2jr $ra # returns

Example

Page 54: 1 Abstractions and Computers and the MAL programming Language

54CMPE12c Gabriel Hugh Elkaim

Operating System Calls

Use $2 ($v0) to pass code to OSUse $4 ($a0) to pass data to OSUse “syscall” instruction to call OS

Very tedious and dangerous for a programmer to deal with IO. This is why we like to have an OS. Need an instruction though to get its attention.

Page 55: 1 Abstractions and Computers and the MAL programming Language

55CMPE12c Gabriel Hugh Elkaim

Code ($v0) Function Usage & Result

1 Print Integer Put integer in $a0

2Print Float

Put floating point number in $f12

3 Print Double Put double in $f12

4 Print String Put address of string into $a0

5 Read Integer Returns integer read in $v0

6 Read Float Returns float read in $f0

7 Read Double Returns double read in $f0

8Read String

Put address in $a0, length in $a1

10 Exit Quits program

11 Print Character

Put character in $a0

12 Get Character Returns character in $v0

Operating System Codes

Page 56: 1 Abstractions and Computers and the MAL programming Language

56CMPE12c Gabriel Hugh Elkaim

To print a character# address of the char must be in $s0lb $a0, ($s0)# $4 char to be printedli $v0, 11 # code for putcsyscall

To read in a character

li $v0, 12 # code for getcsyscall # character returned

# in $v0

SYS Calls Examples

Page 57: 1 Abstractions and Computers and the MAL programming Language

57CMPE12c Gabriel Hugh Elkaim

To end your program

li $v0, 10 # code for donesyscall

SYS Calls Examples

Page 58: 1 Abstractions and Computers and the MAL programming Language

58CMPE12c Gabriel Hugh Elkaim

Page 59: 1 Abstractions and Computers and the MAL programming Language

59CMPE12c Gabriel Hugh Elkaim

Page 60: 1 Abstractions and Computers and the MAL programming Language

60CMPE12c Gabriel Hugh Elkaim