SlideShare une entreprise Scribd logo
1  sur  39
Lecture 2
Computer Organization and
Assembly Language
Fall 2013
Two’s Compliment
Multiplication
Division
Multiplication
• sign extend both integers to twice as many bits.
• Then take bits from the LSB of the result.
Example 1:
4-bit, 2's complement Multiplication:
Both negative
-1 x -7 = 7 (Decimal)
-1 = 1111(Binary) Sign Extended = 1111 1111
-7 = 1001(Binary) Sign Extended = 1111 1001
Cont….
11111111 (-1)
x
11110011 (-7)
11111111
00000000
00000000
11111111
11111111
11111111
11111111
+ 11111111
1 000 00000111 (7)
bits ignored
bits that should be considered
Cont....
Example 2:
Negative and Positive:
Decimal:
3 x (-5) = -15
Binary:
11110001
Cont….
Without Sign Extension
0011 (3)
x 1011 (-5)
0011
0011
0000
+ 0011
0100001 (Wrong)

•

With Sign Extension
0000 0011 (3)

x 1111 1011 (-5)
00000011
00000011
00000000
00000011
0000 0011
0000 0011
0000 0011
+ 0000 0011
1011110001 = - 15
Number of LSB’s taken = Total number of bits of operand
Division
• Let dividend by X and divisor be Y. When we
divide the unsigned format number (integers
non fractional numbers)
• Dividend (X) /Divisor (Y)
Cont….
1. Set the initial quotient Q = 0000.
2.Check if X < Y, if yes, then Q is
unchanged and R = X. Stop the process.
3.If X >= Y, increment the quotient. [New
Q in first cycle is Q= 0001, second cycle it
will be Q= 0010.]
4. Find X – Y using two’s complement arithmetic and get
the R.
5. Set X = R. Repeat steps 2 to 5 till X < Y.
6. Now Q is the result for the quotient and
R = finally left X is the final remainder.
Example 1: X ÷ Y = 0111 ÷ 0011
• Here X > Y.
Step 1: Q = 0000
Step 2: X >= Y and so go to next step.
Step 3: Q = 0001
Step 4: Find X – Y = 0111 – 0011 = 0111 + 1101 =
0100. R = 0100.
Step 5: X = R = 0100.
Example 1: X ÷ Y = 0111 ÷ 0011
Step 6: Repeat steps 2 to 5.
We get Q = 0001 + 1 = 0010 and R = 0001.

Answer is Q = 0010 (decimal 2) and
R = 0001 (decimal 1) as expected from division
of 7 by 3
Example 2: 117/9 = 13
1101
1001) 1110101
1001
1011
1001
1001
1001
xxxx
1101 = 13
Integer Arithmetic
• MIPS Architecture and Assembly Language
Overview
• Data Types and Literals
• Registers
• Program Structure
– Data Declarations
– Code
– Comments

• Data Declarations
• Load / Store Instructions
Integer Arithmetic
• Addressing
• Indirect Addressing
• Based or Indexed Addressing

• Arithmetic Instructions
• Control Structures
• Branches
• Jumps
• Subroutine Calls

• System Calls and I/O (SPIM Simulator)
Integer Arithmetic
• Data Types and Literals
• Data types:
–
–
–
–

Instructions are all 32 bits
byte(8 bits), halfword (2 bytes), word (4 bytes)
a character requires 1 byte of storage
an integer requires 1 word (4 bytes) of storage

• Literals:
– numbers entered as is. e.g. 4
– characters enclosed in single quotes. e.g. 'b'
– strings enclosed in double quotes. e.g. "A string"
Integer Arithmetic
• Registers
– 32 general-purpose registers
– register preceded by $ in assembly language instruction
two formats for addressing:
• using register number e.g. $0 through $31
• using equivalent names e.g. $t1, $sp

– special registers Lo and Hi used to store result of
multiplication and division
– not directly addressable; contents accessed with special
instruction mfhi ("move from Hi") and mflo ("move from
Lo")
– stack grows from high memory to low memory
Register
Number

Alternative
Name

Description

0

zero

the value 0

1

$at

(assembler temporary) reserved by the assembler

2-3

$v0-$v1

(values) from expression evaluation and function results

4-7

$a0-$a3

1st 4 arguments for subroutine. Not preserved across procedure
calls

8-15

$t0-$t7

(temporaries) Caller saved if needed. Subroutines can use w/out
saving. Not preserved across procedure calls

16-23

$s0-$s7

(saved values) - Callee saved. A subroutine using one of these must
save original and restore it before exiting. Preserved across
procedure calls

24-25

$t8-$t9

(temporaries) Caller saved if needed. Subroutines can use w/out
saving. These are in addition to $t0 - $t7 above. Not preserved
across procedure calls.

26-27

$k0-$k1

reserved for use by the interrupt/trap handler

28

$gp

global pointer. Points to the middle of the 64K block of memory in
the static data segment
Register
Number

Alternative
Name

29

$sp

30

$s8/$fp

31

$ra

Description
stack pointer. Points to last location on the stack.

saved value / frame pointer. Preserved across procedure
calls
return address
Integer Arithmetic
• Program Structure
– just plain text file with data declarations, program code
(name of file should end in suffix .s to be used with SPIM
simulator)
– data declaration section followed by program code section

• Data Declarations
• placed in section of program identified with assembler
directive .data
• declares variable names used in program; storage
allocated in main memory (RAM)
Integer Arithmetic
• Code
• placed in section of text identified with assembler directive

.text
• contains program code (instructions)
• starting point for code e.g. execution given label main:
• ending point of main code should use exit system call (see
in next slides)
Integer Arithmetic
• Comments
• anything following # on a line
# This stuff would be considered a comment
• Template for a MIPS assembly language program:
• # Comment giving name of program and description of
function
• # Template.s
• # Bare-bones outline of MIPS assembly language program
Integer Arithmetic
.data
.text
main:

# variable declarations follow this line
# ... .
# instructions follow this line
# indicates start of code (first instruction to
execute)
# ...
# End of program, leave a blank line afterwards
to make SPIM happy
Integer Arithmetic
• Data Declarations
Format for declarations:
name:
storage_type

value(s)

– create storage for variable of specified type with given
name and specified value
– value(s) usually gives initial value(s); for storage type
.space, gives number of spaces to be allocated
•
•

Note: labels always followed by colon ( : )
Example code
var1:
.word 3
# create a single integer variable with initial value 3
array1: .byte 'a','b' # create a 2-element character array with elements initialized # to a and b
array2: .space 40 # allocate 40 consecutive bytes, with storage uninitialized
# could be used as a 40-element character array, or a
# 10-element integer array; a comment should indicate
which!
Integer Arithmetic
• Load / Store Instructions
– RAM access only allowed with load and store instructions
– all other instructions use register operands

• Format for Load:
•

lw register _destination, RAM _source

•

lb register_destination, RAM_source

#copy word (4 bytes) at source RAM location
to destination register.
#copy byte at source RAM location to low-order
byte of destination register, and sign-extend to
higher-order bytes .

• Format for Store:
•

sw register_source, RAM_destination

•

sb register_source, RAM_destination

#store word in source register into RAM
destination
#store byte (low-order) in source register into
RAM destination
Integer Arithmetic
• Load Immediate Format:
li register_destination, value #load immediate value into destination register

Example:
var1:

.data
.word 23 # declare storage for var1; initial value is 23
.text

__start:
lw $t0, var1 # load contents of RAM location into register $t0: $t0 = var1
li $t1, 5
# $t1 = 5 ("load immediate")
sw $t1, var1 # store contents of register $t1 into RAM: var1 = $t1
done
Integer Arithmetic
• Indirect and Based Addressing
– Used only with load and store instructions

• Load Address:
la $t0, var1 #copy RAM address of var1 (presumably a label defined in the
program) into register $t0

• Indirect Addressing:
lw $t2, ($t0) #load word at RAM address contained in $t0 into $t2
sw $t2, ($t0) #store word in register $t2 into RAM at address contained in $t0
Integer Arithmetic
• Based or Indexed Addressing:
lw $t2, 4($t0)

#load word at RAM address ($t0+4) into register $t2. "4"
gives offset from address in register $t0
sw $t2, -12($t0) #store word in register $t2 into RAM at address ($t0 - 12)
negative offsets are fine
Note: based addressing is especially useful for:
arrays: access elements as offset from base address
stacks: easy to access elements at offset from stack pointer or frame pointer
Integer Arithmetic
Example:
.data
array1: .space 12 # declare 12 bytes of storage to hold array of 3 integers
.text
__start: la $t0, array1 # load base address of array into register $t0
li $t1, 5 # $t1 = 5 ("load immediate")
sw $t1, ($t0) # first array element set to 5; indirect addressing
li $t1, 13 # $t1 = 13
sw $t1, 4($t0) # second array element set to 13
li $t1, -7 # $t1 = -7
sw $t1, 8($t0) # third array element set to -7
done
Integer Arithmetic
• Arithmetic Instructions
– most use 3 operands
– all operands are registers; no RAM or indirect addressing
– operand size is word (4 bytes)
Integer Arithmetic
add $t0,$t1,$t2
sub $t2,$t3,$t4
addi $t2,$t3, 5
addu $t1,$t6,$t7
subu $t1,$t6,$t7
mult $t3,$t4
div $t5,$t6
mfhi $t0
mflo $t1
move $t2,$t3

# $t0 = $t1 + $t2; add as signed (2's complement) integers
# $t2 = $t3 Ð $t4
# $t2 = $t3 + 5; "add immediate" (no sub immediate)
# $t1 = $t6 + $t7; add as unsigned integers
# $t1 = $t6 + $t7; subtract as unsigned integers
# multiply 32-bit quantities in $t3 and $t4, and store 64-bit
# result in special registers Lo and Hi: (Hi,Lo) = $t3 * $t4
# Lo = $t5 / $t6 (integer quotient)
# Hi = $t5 mod $t6 (remainder)
# move quantity in special register Hi to $t0: $t0 = Hi
# move quantity in special register Lo to $t1: $t1 = Lo
# used to get at result of product or quotient
# $t2 = $t3
Integer Arithmetic
• Control Structures
• Branches:
• comparison for conditional branches is built into
instruction
b target
beq $t0,$t1,target
blt $t0,$t1,target
ble $t0,$t1,target
bgt $t0,$t1,target
bge $t0,$t1,target
bne $t0,$t1,target

# unconditional branch to program label target
# branch to target if $t0 = $t1
# branch to target if $t0 < $t1
# branch to target if $t0 <= $t1
# branch to target if $t0 > $t1
# branch to target if $t0 >= $t1
# branch to target if $t0 <> $t1
Integer Arithmetic
• Jumps :
j target
jr $t3

# unconditional jump to program label target
# jump to address contained in $t3 ("jump register")

• Subroutine Calls :
•
•

subroutine call: "jump and link" instruction
jal sub_label
# "jump and link"
copy program counter (return address) to register $ra (return address register)
jump to program statement at sub_label

subroutine return: "jump register" instruction
jr $ra # "jump register"
• jump to return address in $ra (stored by jal instruction)
Note: return address stored in register $ra; if subroutine calls other subroutines, or is recursive,
return address should be copied from $ra onto stack to preserve it, since jal always places
return address in this register and hence will overwrite previous value
Integer Arithmetic
• System Calls and I/O (SPIM Simulator)
– used to read or print values or strings from input/output window, and
indicate program end
– use syscall operating system routine call
– first supply appropriate values in registers $v0 and $a0-$a1
– result value (if any) returned in register $v0
Service

Code in $v0

Arguments

Results

print_int

1

$a0 = integer to be printed

print_float

2

$f12 = float to be printed

print_double

3

$f12 = double to be printed

print_string

4

$a0 = address of string in memory

read_int

5

integer returned
in $v0

read_float

6

Float returned in
$v0

read_double

7

double returned
in $v0

read_string

8

$a0 = memory address of string input
buffer
$a1 = length of string buffer (n)

sbrk

9

$a0 = amount

exit

10
Integer Arithmetic
– The print_string service expects the address to start a null-terminated
character string. The directive .asciiz creates a null-terminated
character string.
– The read_int, read_float and read_double services read an entire line
of input up to and including the newline character.
– The read_string service has the same semantices as the UNIX library
routine fgets.
• It reads up to n-1 characters into a buffer and terminates the string with a null
character.
• If fewer than n-1 characters are in the current line, it reads up to and including
the newline and terminates the string with a null character.

– The sbrk service returns the address to a block of memory containing
n additional bytes. This would be used for dynamic memory allocation.
– The exit service stops a program from running.
Integer Arithmetic
e.g. Print out integer value contained in register $t2
li $v0, 1 # load appropriate system call code into register $v0;
# code for printing integer is 1
move $a0, $t2 # move integer to be printed into $a0: $a0 =
$t2
syscall # call operating system to perform operation
Integer Arithmetic
e.g. Read integer value, store in RAM location with label
int_value (presumably declared in data section)
li $v0, 5 # load appropriate system call code into register $v0;
# code for reading integer is 5
syscall # call operating system to perform operation
sw $v0, int_value # value read from keyboard returned in
register $v0; # store this in desired
location
Integer Arithmetic
e.g. Print out string (useful for prompts)
.data
string1 .asciiz "Print this.n" # declaration for string variable,
# .asciiz directive makes string null terminated
.text
main: li $v0, 4 # load appropriate system call code into register
$v0;
# code for printing string is 4
la $a0, string1 # load address of string to be printed into $a0
syscall # call operating system to perform print
operation
Integer Arithmetic
e.g. To indicate end of program, use exit system call;
thus last lines of program should be:
li $v0, 10 # system call code for exit = 10
syscall # call operating sys
References
• MIPS Tutorial:
http://logos.cs.uic.edu/366/notes/mips%20qu
ick%20tutorial.htm

Contenu connexe

Tendances

Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
Lecture 2 coding_principles
Lecture 2 coding_principlesLecture 2 coding_principles
Lecture 2 coding_principlesmoduledesign
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming LanguageSteve Johnson
 
Lecture 2 coding_principles
Lecture 2 coding_principlesLecture 2 coding_principles
Lecture 2 coding_principlesmoduledesign
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up ParsingGerwin Ocsena
 
C++ programming program design including data structures
C++ programming program design including data structures C++ programming program design including data structures
C++ programming program design including data structures Ahmad Idrees
 
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiRaspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiMohamed Abdallah
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & ImportMohd Sajjad
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshopneosphere
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)Saifur Rahman
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 

Tendances (19)

Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
 
What is c
What is cWhat is c
What is c
 
Lecture 2 coding_principles
Lecture 2 coding_principlesLecture 2 coding_principles
Lecture 2 coding_principles
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Lecture 2 coding_principles
Lecture 2 coding_principlesLecture 2 coding_principles
Lecture 2 coding_principles
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
C++ programming program design including data structures
C++ programming program design including data structures C++ programming program design including data structures
C++ programming program design including data structures
 
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiRaspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry Pi
 
Python-02| Input, Output & Import
Python-02| Input, Output & ImportPython-02| Input, Output & Import
Python-02| Input, Output & Import
 
C programming Workshop
C programming WorkshopC programming Workshop
C programming Workshop
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
C++
C++C++
C++
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Unit 3. Input and Output
Unit 3. Input and OutputUnit 3. Input and Output
Unit 3. Input and Output
 

En vedette

Enunciados Descritivos Forum 1
Enunciados Descritivos Forum 1Enunciados Descritivos Forum 1
Enunciados Descritivos Forum 1guestbfb743
 
Tabela D1 Ana Soares
Tabela D1 Ana SoaresTabela D1 Ana Soares
Tabela D1 Ana Soaresguestbfb743
 
Tabela Matriz Novo Curso[1]
Tabela Matriz   Novo Curso[1]Tabela Matriz   Novo Curso[1]
Tabela Matriz Novo Curso[1]guestbfb743
 
Implementation of 16 x16 bit multiplication algorithm by using vedic mathemat...
Implementation of 16 x16 bit multiplication algorithm by using vedic mathemat...Implementation of 16 x16 bit multiplication algorithm by using vedic mathemat...
Implementation of 16 x16 bit multiplication algorithm by using vedic mathemat...eSAT Journals
 
O Modelo De Auto Ana Soares
O Modelo De Auto Ana SoaresO Modelo De Auto Ana Soares
O Modelo De Auto Ana Soaresguestbfb743
 
AI- memory organisation systems
AI-  memory organisation systemsAI-  memory organisation systems
AI- memory organisation systemsratikaagarwal
 
Webデザインのための配色セオリー
Webデザインのための配色セオリーWebデザインのための配色セオリー
Webデザインのための配色セオリーwebcampusschoo
 
Associative memory 14208
Associative memory 14208Associative memory 14208
Associative memory 14208Ameer Mehmood
 
Booths Multiplication Algorithm
Booths Multiplication AlgorithmBooths Multiplication Algorithm
Booths Multiplication Algorithmknightnick
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanPost Planner
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionIn a Rocket
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting PersonalKirsty Hulse
 

En vedette (18)

Enunciados Descritivos Forum 1
Enunciados Descritivos Forum 1Enunciados Descritivos Forum 1
Enunciados Descritivos Forum 1
 
Tabela D1 Ana Soares
Tabela D1 Ana SoaresTabela D1 Ana Soares
Tabela D1 Ana Soares
 
Maabe.Doc
Maabe.DocMaabe.Doc
Maabe.Doc
 
Tabela Matriz Novo Curso[1]
Tabela Matriz   Novo Curso[1]Tabela Matriz   Novo Curso[1]
Tabela Matriz Novo Curso[1]
 
Implementation of 16 x16 bit multiplication algorithm by using vedic mathemat...
Implementation of 16 x16 bit multiplication algorithm by using vedic mathemat...Implementation of 16 x16 bit multiplication algorithm by using vedic mathemat...
Implementation of 16 x16 bit multiplication algorithm by using vedic mathemat...
 
O Modelo De Auto Ana Soares
O Modelo De Auto Ana SoaresO Modelo De Auto Ana Soares
O Modelo De Auto Ana Soares
 
AI- memory organisation systems
AI-  memory organisation systemsAI-  memory organisation systems
AI- memory organisation systems
 
Booth algorithm
Booth algorithmBooth algorithm
Booth algorithm
 
Webデザインのための配色セオリー
Webデザインのための配色セオリーWebデザインのための配色セオリー
Webデザインのための配色セオリー
 
Associative memory 14208
Associative memory 14208Associative memory 14208
Associative memory 14208
 
Booths Multiplication Algorithm
Booths Multiplication AlgorithmBooths Multiplication Algorithm
Booths Multiplication Algorithm
 
Memory Organization
Memory OrganizationMemory Organization
Memory Organization
 
Memory hierarchy
Memory hierarchyMemory hierarchy
Memory hierarchy
 
Virtual memory ppt
Virtual memory pptVirtual memory ppt
Virtual memory ppt
 
Instruction cycle
Instruction cycleInstruction cycle
Instruction cycle
 
How to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media PlanHow to Build a Dynamic Social Media Plan
How to Build a Dynamic Social Media Plan
 
Learn BEM: CSS Naming Convention
Learn BEM: CSS Naming ConventionLearn BEM: CSS Naming Convention
Learn BEM: CSS Naming Convention
 
SEO: Getting Personal
SEO: Getting PersonalSEO: Getting Personal
SEO: Getting Personal
 

Similaire à Lecture 2 coal sping12

Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsYashJain47002
 
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...Hsien-Hsin Sean Lee, Ph.D.
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golangwww.ixxo.io
 
Chapter 2 Part2 C
Chapter 2 Part2 CChapter 2 Part2 C
Chapter 2 Part2 Cececourse
 
C:\Fakepath\Chapter 2 Part2 B
C:\Fakepath\Chapter 2 Part2 BC:\Fakepath\Chapter 2 Part2 B
C:\Fakepath\Chapter 2 Part2 Bececourse
 
Instruction Set Architecture: MIPS
Instruction Set Architecture: MIPSInstruction Set Architecture: MIPS
Instruction Set Architecture: MIPSPrasenjit Dey
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
Programming with 8085.pptx
Programming with 8085.pptxProgramming with 8085.pptx
Programming with 8085.pptxSachinKupade
 

Similaire à Lecture 2 coal sping12 (20)

Mips
MipsMips
Mips
 
MIPS Architecture
MIPS ArchitectureMIPS Architecture
MIPS Architecture
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Lec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questionsLec2_cont.pptx galgotias University questions
Lec2_cont.pptx galgotias University questions
 
1.2 matlab numerical data
1.2  matlab numerical data1.2  matlab numerical data
1.2 matlab numerical data
 
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
Lec19 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Pr...
 
Introduction to Computing
Introduction to ComputingIntroduction to Computing
Introduction to Computing
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golang
 
Chapter 2 Part2 C
Chapter 2 Part2 CChapter 2 Part2 C
Chapter 2 Part2 C
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
C:\Fakepath\Chapter 2 Part2 B
C:\Fakepath\Chapter 2 Part2 BC:\Fakepath\Chapter 2 Part2 B
C:\Fakepath\Chapter 2 Part2 B
 
Instruction Set Architecture: MIPS
Instruction Set Architecture: MIPSInstruction Set Architecture: MIPS
Instruction Set Architecture: MIPS
 
Python
PythonPython
Python
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
Programming with 8085.pptx
Programming with 8085.pptxProgramming with 8085.pptx
Programming with 8085.pptx
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 
Python basics
Python basicsPython basics
Python basics
 

Dernier

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 

Dernier (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

Lecture 2 coal sping12

  • 1. Lecture 2 Computer Organization and Assembly Language Fall 2013
  • 3. Multiplication • sign extend both integers to twice as many bits. • Then take bits from the LSB of the result. Example 1: 4-bit, 2's complement Multiplication: Both negative -1 x -7 = 7 (Decimal) -1 = 1111(Binary) Sign Extended = 1111 1111 -7 = 1001(Binary) Sign Extended = 1111 1001
  • 4. Cont…. 11111111 (-1) x 11110011 (-7) 11111111 00000000 00000000 11111111 11111111 11111111 11111111 + 11111111 1 000 00000111 (7) bits ignored bits that should be considered
  • 5. Cont.... Example 2: Negative and Positive: Decimal: 3 x (-5) = -15 Binary: 11110001
  • 6. Cont…. Without Sign Extension 0011 (3) x 1011 (-5) 0011 0011 0000 + 0011 0100001 (Wrong) • With Sign Extension 0000 0011 (3) x 1111 1011 (-5) 00000011 00000011 00000000 00000011 0000 0011 0000 0011 0000 0011 + 0000 0011 1011110001 = - 15 Number of LSB’s taken = Total number of bits of operand
  • 7. Division • Let dividend by X and divisor be Y. When we divide the unsigned format number (integers non fractional numbers) • Dividend (X) /Divisor (Y)
  • 8. Cont…. 1. Set the initial quotient Q = 0000. 2.Check if X < Y, if yes, then Q is unchanged and R = X. Stop the process. 3.If X >= Y, increment the quotient. [New Q in first cycle is Q= 0001, second cycle it will be Q= 0010.] 4. Find X – Y using two’s complement arithmetic and get the R. 5. Set X = R. Repeat steps 2 to 5 till X < Y. 6. Now Q is the result for the quotient and R = finally left X is the final remainder.
  • 9. Example 1: X ÷ Y = 0111 ÷ 0011 • Here X > Y. Step 1: Q = 0000 Step 2: X >= Y and so go to next step. Step 3: Q = 0001 Step 4: Find X – Y = 0111 – 0011 = 0111 + 1101 = 0100. R = 0100. Step 5: X = R = 0100.
  • 10. Example 1: X ÷ Y = 0111 ÷ 0011 Step 6: Repeat steps 2 to 5. We get Q = 0001 + 1 = 0010 and R = 0001. Answer is Q = 0010 (decimal 2) and R = 0001 (decimal 1) as expected from division of 7 by 3
  • 11. Example 2: 117/9 = 13 1101 1001) 1110101 1001 1011 1001 1001 1001 xxxx 1101 = 13
  • 12. Integer Arithmetic • MIPS Architecture and Assembly Language Overview • Data Types and Literals • Registers • Program Structure – Data Declarations – Code – Comments • Data Declarations • Load / Store Instructions
  • 13. Integer Arithmetic • Addressing • Indirect Addressing • Based or Indexed Addressing • Arithmetic Instructions • Control Structures • Branches • Jumps • Subroutine Calls • System Calls and I/O (SPIM Simulator)
  • 14. Integer Arithmetic • Data Types and Literals • Data types: – – – – Instructions are all 32 bits byte(8 bits), halfword (2 bytes), word (4 bytes) a character requires 1 byte of storage an integer requires 1 word (4 bytes) of storage • Literals: – numbers entered as is. e.g. 4 – characters enclosed in single quotes. e.g. 'b' – strings enclosed in double quotes. e.g. "A string"
  • 15. Integer Arithmetic • Registers – 32 general-purpose registers – register preceded by $ in assembly language instruction two formats for addressing: • using register number e.g. $0 through $31 • using equivalent names e.g. $t1, $sp – special registers Lo and Hi used to store result of multiplication and division – not directly addressable; contents accessed with special instruction mfhi ("move from Hi") and mflo ("move from Lo") – stack grows from high memory to low memory
  • 16. Register Number Alternative Name Description 0 zero the value 0 1 $at (assembler temporary) reserved by the assembler 2-3 $v0-$v1 (values) from expression evaluation and function results 4-7 $a0-$a3 1st 4 arguments for subroutine. Not preserved across procedure calls 8-15 $t0-$t7 (temporaries) Caller saved if needed. Subroutines can use w/out saving. Not preserved across procedure calls 16-23 $s0-$s7 (saved values) - Callee saved. A subroutine using one of these must save original and restore it before exiting. Preserved across procedure calls 24-25 $t8-$t9 (temporaries) Caller saved if needed. Subroutines can use w/out saving. These are in addition to $t0 - $t7 above. Not preserved across procedure calls. 26-27 $k0-$k1 reserved for use by the interrupt/trap handler 28 $gp global pointer. Points to the middle of the 64K block of memory in the static data segment
  • 17. Register Number Alternative Name 29 $sp 30 $s8/$fp 31 $ra Description stack pointer. Points to last location on the stack. saved value / frame pointer. Preserved across procedure calls return address
  • 18. Integer Arithmetic • Program Structure – just plain text file with data declarations, program code (name of file should end in suffix .s to be used with SPIM simulator) – data declaration section followed by program code section • Data Declarations • placed in section of program identified with assembler directive .data • declares variable names used in program; storage allocated in main memory (RAM)
  • 19. Integer Arithmetic • Code • placed in section of text identified with assembler directive .text • contains program code (instructions) • starting point for code e.g. execution given label main: • ending point of main code should use exit system call (see in next slides)
  • 20. Integer Arithmetic • Comments • anything following # on a line # This stuff would be considered a comment • Template for a MIPS assembly language program: • # Comment giving name of program and description of function • # Template.s • # Bare-bones outline of MIPS assembly language program
  • 21. Integer Arithmetic .data .text main: # variable declarations follow this line # ... . # instructions follow this line # indicates start of code (first instruction to execute) # ... # End of program, leave a blank line afterwards to make SPIM happy
  • 22. Integer Arithmetic • Data Declarations Format for declarations: name: storage_type value(s) – create storage for variable of specified type with given name and specified value – value(s) usually gives initial value(s); for storage type .space, gives number of spaces to be allocated • • Note: labels always followed by colon ( : ) Example code var1: .word 3 # create a single integer variable with initial value 3 array1: .byte 'a','b' # create a 2-element character array with elements initialized # to a and b array2: .space 40 # allocate 40 consecutive bytes, with storage uninitialized # could be used as a 40-element character array, or a # 10-element integer array; a comment should indicate which!
  • 23. Integer Arithmetic • Load / Store Instructions – RAM access only allowed with load and store instructions – all other instructions use register operands • Format for Load: • lw register _destination, RAM _source • lb register_destination, RAM_source #copy word (4 bytes) at source RAM location to destination register. #copy byte at source RAM location to low-order byte of destination register, and sign-extend to higher-order bytes . • Format for Store: • sw register_source, RAM_destination • sb register_source, RAM_destination #store word in source register into RAM destination #store byte (low-order) in source register into RAM destination
  • 24. Integer Arithmetic • Load Immediate Format: li register_destination, value #load immediate value into destination register Example: var1: .data .word 23 # declare storage for var1; initial value is 23 .text __start: lw $t0, var1 # load contents of RAM location into register $t0: $t0 = var1 li $t1, 5 # $t1 = 5 ("load immediate") sw $t1, var1 # store contents of register $t1 into RAM: var1 = $t1 done
  • 25. Integer Arithmetic • Indirect and Based Addressing – Used only with load and store instructions • Load Address: la $t0, var1 #copy RAM address of var1 (presumably a label defined in the program) into register $t0 • Indirect Addressing: lw $t2, ($t0) #load word at RAM address contained in $t0 into $t2 sw $t2, ($t0) #store word in register $t2 into RAM at address contained in $t0
  • 26. Integer Arithmetic • Based or Indexed Addressing: lw $t2, 4($t0) #load word at RAM address ($t0+4) into register $t2. "4" gives offset from address in register $t0 sw $t2, -12($t0) #store word in register $t2 into RAM at address ($t0 - 12) negative offsets are fine Note: based addressing is especially useful for: arrays: access elements as offset from base address stacks: easy to access elements at offset from stack pointer or frame pointer
  • 27. Integer Arithmetic Example: .data array1: .space 12 # declare 12 bytes of storage to hold array of 3 integers .text __start: la $t0, array1 # load base address of array into register $t0 li $t1, 5 # $t1 = 5 ("load immediate") sw $t1, ($t0) # first array element set to 5; indirect addressing li $t1, 13 # $t1 = 13 sw $t1, 4($t0) # second array element set to 13 li $t1, -7 # $t1 = -7 sw $t1, 8($t0) # third array element set to -7 done
  • 28. Integer Arithmetic • Arithmetic Instructions – most use 3 operands – all operands are registers; no RAM or indirect addressing – operand size is word (4 bytes)
  • 29. Integer Arithmetic add $t0,$t1,$t2 sub $t2,$t3,$t4 addi $t2,$t3, 5 addu $t1,$t6,$t7 subu $t1,$t6,$t7 mult $t3,$t4 div $t5,$t6 mfhi $t0 mflo $t1 move $t2,$t3 # $t0 = $t1 + $t2; add as signed (2's complement) integers # $t2 = $t3 Ð $t4 # $t2 = $t3 + 5; "add immediate" (no sub immediate) # $t1 = $t6 + $t7; add as unsigned integers # $t1 = $t6 + $t7; subtract as unsigned integers # multiply 32-bit quantities in $t3 and $t4, and store 64-bit # result in special registers Lo and Hi: (Hi,Lo) = $t3 * $t4 # Lo = $t5 / $t6 (integer quotient) # Hi = $t5 mod $t6 (remainder) # move quantity in special register Hi to $t0: $t0 = Hi # move quantity in special register Lo to $t1: $t1 = Lo # used to get at result of product or quotient # $t2 = $t3
  • 30. Integer Arithmetic • Control Structures • Branches: • comparison for conditional branches is built into instruction b target beq $t0,$t1,target blt $t0,$t1,target ble $t0,$t1,target bgt $t0,$t1,target bge $t0,$t1,target bne $t0,$t1,target # unconditional branch to program label target # branch to target if $t0 = $t1 # branch to target if $t0 < $t1 # branch to target if $t0 <= $t1 # branch to target if $t0 > $t1 # branch to target if $t0 >= $t1 # branch to target if $t0 <> $t1
  • 31. Integer Arithmetic • Jumps : j target jr $t3 # unconditional jump to program label target # jump to address contained in $t3 ("jump register") • Subroutine Calls : • • subroutine call: "jump and link" instruction jal sub_label # "jump and link" copy program counter (return address) to register $ra (return address register) jump to program statement at sub_label subroutine return: "jump register" instruction jr $ra # "jump register" • jump to return address in $ra (stored by jal instruction) Note: return address stored in register $ra; if subroutine calls other subroutines, or is recursive, return address should be copied from $ra onto stack to preserve it, since jal always places return address in this register and hence will overwrite previous value
  • 32. Integer Arithmetic • System Calls and I/O (SPIM Simulator) – used to read or print values or strings from input/output window, and indicate program end – use syscall operating system routine call – first supply appropriate values in registers $v0 and $a0-$a1 – result value (if any) returned in register $v0
  • 33. Service Code in $v0 Arguments Results print_int 1 $a0 = integer to be printed print_float 2 $f12 = float to be printed print_double 3 $f12 = double to be printed print_string 4 $a0 = address of string in memory read_int 5 integer returned in $v0 read_float 6 Float returned in $v0 read_double 7 double returned in $v0 read_string 8 $a0 = memory address of string input buffer $a1 = length of string buffer (n) sbrk 9 $a0 = amount exit 10
  • 34. Integer Arithmetic – The print_string service expects the address to start a null-terminated character string. The directive .asciiz creates a null-terminated character string. – The read_int, read_float and read_double services read an entire line of input up to and including the newline character. – The read_string service has the same semantices as the UNIX library routine fgets. • It reads up to n-1 characters into a buffer and terminates the string with a null character. • If fewer than n-1 characters are in the current line, it reads up to and including the newline and terminates the string with a null character. – The sbrk service returns the address to a block of memory containing n additional bytes. This would be used for dynamic memory allocation. – The exit service stops a program from running.
  • 35. Integer Arithmetic e.g. Print out integer value contained in register $t2 li $v0, 1 # load appropriate system call code into register $v0; # code for printing integer is 1 move $a0, $t2 # move integer to be printed into $a0: $a0 = $t2 syscall # call operating system to perform operation
  • 36. Integer Arithmetic e.g. Read integer value, store in RAM location with label int_value (presumably declared in data section) li $v0, 5 # load appropriate system call code into register $v0; # code for reading integer is 5 syscall # call operating system to perform operation sw $v0, int_value # value read from keyboard returned in register $v0; # store this in desired location
  • 37. Integer Arithmetic e.g. Print out string (useful for prompts) .data string1 .asciiz "Print this.n" # declaration for string variable, # .asciiz directive makes string null terminated .text main: li $v0, 4 # load appropriate system call code into register $v0; # code for printing string is 4 la $a0, string1 # load address of string to be printed into $a0 syscall # call operating system to perform print operation
  • 38. Integer Arithmetic e.g. To indicate end of program, use exit system call; thus last lines of program should be: li $v0, 10 # system call code for exit = 10 syscall # call operating sys