informationsteknologi friday, september 28, 2007computer architecture i - class 21 today’s class...

13
Informationsteknologi Friday, September 28, 2007 Computer Architecture I - Class 2 1 Today’s class More assembly language programming

Upload: brent-harris

Post on 05-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 1

Today’s class

More assembly language programming

Page 2: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 2

Data transfer instructions

lw – load word from memory into a register (note that the address must be a word address, divisible by 4)

lb – load byte sw – store word into memory from a

register (address must be for a word) sb – store byte

Page 3: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 3

Addition (signed) add regdest,regsrc1,regsrc2

Performs regdest = regsrc1 + regsrc2

Uses register addressing (no memory reference) addi regdest,regsrc,const

Performs regdest = regsrc + const Add immediate, because const is part of instruction (no memory

reference) add regdest,regsrc,const

Will generate an addi instruction by the assembler add regdest, const

Will generate addi regdest,regdest,const

Page 4: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 4

Addition (unsigned)

addu regdest,regsrc1,regsrc2

Performs regdest = regsrc1 + regsrc2

addiu regdest,regsrc,const Performs regdest = regsrc + const

addu regdest,regsrc,const Will generate an addiu instruction by the assembler

addu regdest, const Will generate addiu regdest,regdest,const

Page 5: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 5

Subtraction

sub regdest,regsrc1,regsrc2

Performs regdest = regsrc1 - regsrc2

This is a signed subtraction

subu regdest,regsrc1,regsrc2

Performs regdest = regsrc1 - regsrc2

This is an unsigned subtraction

No subi instruction Can do an addi with the negative of what you want

to subtract

Page 6: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 6

Multiplication Result of multiplying two 32-bit numbers can be 64 bits

big Have two special registers called lo and hi to hold the

two 32-bit parts of the result mult regsrc1,regsrc2 will put the low 32 bits of the product

in lo and the high 32 bits in hi Use mflo regdest (move from lo) to get the low 32 bits

into a register Use mfhi regdest (move from hi) to get the high 32 bits

into a register The pseudo-instruction mul regdest,regsrc1,regsrc2 will get

the low 32 bits of the product into regdest

Page 7: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 7

Division

div regsrc1,regsrc2 will put the integer quotient of regsrc1/regsrc2 in lo and the remainder in hi

The pseudo-instruction div regdest,regsrc1,regsrc2 will get the integer quotient into regdest

Page 8: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 8

Example program

Program temperature.asm converts Celsius temperatures to Fahrenheit

Study it and demo it

Page 9: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 9

In-class exercise

Modify the temperature conversion program so it converts from Fahrenheit to Celsius instead.

)32(9

5 FahrenheitCelsius

Page 10: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 10

Unconditional branching

j addr – jumps to the indicated address and continues execution from there

jr reg – jumps to the address stored in the specified register and continues execution from there

Page 11: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 11

Conditional branching beq regsrc1,regsrc2,addr – branch to addr if regsrc1 equals

regsrc2

beqz reg,addr – branch to addr if reg equals 0 bne regsrc1,regsrc2,addr – branch to addr if regsrc1 does not

equal regsrc2

blt regsrc1,regsrc2,addr – branch to addr if regsrc1 is less than regsrc2

The assembler will let the second operand be a constant instead of a register if desired

There are many more – see Appendix B (page 146) of Waldron

Page 12: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 12

Example programs

Program length.asm prints out the length of a character string

Program replace.asm replaces lower case ‘a’ with upper case ‘A’ in a string

Study them and demo them

Page 13: Informationsteknologi Friday, September 28, 2007Computer Architecture I - Class 21 Today’s class More assembly language programming

Info

rmat

ions

tekn

olog

i

Friday, September 28, 2007 Computer Architecture I - Class 2 13

In-class exercise

Write a program to count how many times the letter ‘e’ appears in a string.