Fall 2020 ECE 445 - Computer Organization 1
BASIC PROGRAMMING IN
ASSEMBLY LANGUAGE
ECE 445 – Computer Organization
Dr. Craig Lorie
Electrical and Computer Engineering
Lecture #5
Fall 2020 ECE 445 - Computer Organization 2
Topics Covered
• Implementing basic HLL constructs in assembly language
• Adding multiple (3 or more) values.
• Reading an 8-bit byte from memory; reading a 32-bit word from memory.
• If-Then-Else
• Loops
• Basic assembly language programming
• Example: Processing an array of integers (e.g. determining the sum)
• Example: Processing a string (e.g. determining its length).
• Introduction to MARS (MIPS Simulator)
• Assembler directives
• Using MARS to write and simulate MIPS assembly language programs.
Fall 2020 ECE 445 - Computer Organization 3
Reading Assignment
• Hennessey / Patterson: A.9
• Hennessey / Patterson: 2.1 – 2.7, 2.10
• MARS Tutorial
• Additional reading posted on Blackboard.
Fall 2020 ECE 445 - Computer Organization 4
MIPS ASSEMBLY LANGUAGE
Instruction Set
Fall 2020 ECE 445 - Computer Organization 5
MIPS Assembly Language Instructions
Fall 2020 ECE 445 - Computer Organization 6
BASIC CONSTRUCTS IN
ASSEMBLY LANGUAGE
Adding multiple (3 or more) values.
Reading a byte from memory; reading a word from memory.
If-Then-Else
Loops
Fall 2020 ECE 445 - Computer Organization 7
Adding Four Values
• C code:
a = b + c + d + e ;
• MIPS assembly language:
add $s0, $s1, $s2 # $s0 = #s1 + $s2
add $s0, $s0, $s3 # $s0 = $s0 + $s3
add $s0, $s0, $s4 # $s0 = $s0 + $s4
# Resulting sum: $s0 = $s1 + $s2 + $s3 + $s4
Register usage: $s0 = a, $s1 = b, $s2 = c, $s3 = d, $s4 = e
Fall 2020 ECE 445 - Computer Organization 8
Reading an 8-bit value from Memory
• C code:
charA = Read(Memory); // read an 8-bit character
• MIPS assembly language: contents of register $t1
lb $t0, 5($t1) # Read byte from address R[$t1]+5
lbu $t0, 5($t1) # Read byte from address R[$t1]+5
Load byte sign-extends the byte; load byte unsigned zero-extends the byte.
Fall 2020 ECE 445 - Computer Organization 9
Reading a 32-bit value from Memory
• C code:
intA = Read(Memory); // read a 32-bit integer
• MIPS assembly language: contents of register $t3
lw $t2, 8($t3) # Read word from address R[$t3]+8
Address of a 32-bit word must be on a word-boundary; must be divisible by 4.
Fall 2020 ECE 445 - Computer Organization 10
IF-THEN-ELSE
• C code:
if (a == b) { c = 1; }
else { c = 0; }
• MIPS assembly language:
beq $s5, $s6, THEN
addi $s7, $zero, 0 # ELSE condition
j Done
THEN: addi $s7, $zero, 1 # THEN condition
Done: … # next instruction
# to execute
Register usage: $s5 = a, $s6 = b, $s7 = c
Fall 2020 ECE 445 - Computer Organization 11
FOR Loop
• C code:
for (int index = 0; index < 10; index++)
{ sum = sum + index; }
• MIPS assembly language:
add $t4, $zero, $zero # initialize index = 0
add $t5, $zero, $zero # initialize sum = 0
addi $t6, $zero, 10 # set upper bound
L1: add $t5, $t5, $t4 # sum = sum + index
addi $t4, $t4, 1 # index = index + 1
bne $t4, $t6, L1 # loop until index = 10
Register usage: $t4 = index, $t5 = sum, $t6 = upper bound
Fall 2020 ECE 445 - Computer Organization 12
WHILE Loop
• C code:
count = 10;
while (count > 0)
{ sum = sum + count; count = count – 1; }
• MIPS assembly language:
addi $s1, $zero, 10 # initialize count = 10
addi $s2, $zero, 0 # initialize sum = 0
L1: beq $s1, $zero, L2 # loop until count = 0
add $s2, $s2, $s1 # sum = sum + count
addi $s1, $s1, -1 # count = count -1
j L1 # jump to top of loop
L2: … # next instruction to execute
Register usage: $s1 = count, $s2 = sum
Fall 2020 ECE 445 - Computer Organization 13
A SIMPLE ASSEMBLY
LANGUAGE PROGRAM
Example: Processing an array of (32-bit) integers
Fall 2020 ECE 445 - Computer Organization 14
Programming in Assembly
• Problem: Calculate the sum of the integers in an array.
• Declare an array with 10 elements; each element is a 32-bit integer.
• Calculate the sum of the first 9 elements of the array.
• Store the sum in the last element of the array.
• C code:
int main ( )
{
int intA[10];
int index, sum = 0;
for ( index = 0; index < 9; index++ )
{ sum = sum + intA[index]; }
intA[9] = sum;
}
Fall 2020 ECE 445 - Computer Organization 15
Programming in Assembly
• MIPS Assembly Language:
.text
.globl main
main: la $t0, intA # load starting address of array
add $t1, $zero, $zero # loop index
addi $t2, $zero, 0 # array offset
addi $t3, $zero, 9 # array size - 1
add $s0, $zero, $zero # sum
loop: lw $s1, 0($t0) # Read 32-bit integer from array
add $s0, $s0, $s1 # sum = sum + A[index]
addi $t1, $t1, 1 # index = index + 1
addi $t0, $t0, 4 # increment address by 4 (4 bytes/word)
bne $t1, $t3, loop # continue for 15 integers
sw $s0, 0($t0) # store sum in A[9]
.data 0x10010000
intA: .word 11, 22, 33, 44, 55, 66, 77, 88, 99, 0
Fall 2020 ECE 445 - Computer Organization 16
Programming in Assembly
• MIPS Assembly Language:
.text • Assembler directives.
.globl main
main: la $t0, intA • Not executable.
add $t1, $zero, $zero
• Directions for the assembler.
addi $t2, $zero, 0
addi $t3, $zero, 9
add $s0, $zero, $zero
loop: lw $s1, 0($t0)
add $s0, $s0, $s1
addi $t1, $t1, 1
addi $t0, $t0, 4
bne $t1, $t3, loop
sw $s0, 0($t0)
.data 0x10010000
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 17
Programming in Assembly
• MIPS Assembly Language:
.text • Labels
.globl main
main: la $t0, intA • Each corresponds to an address
add $t1, $zero, $zero in program memory.
addi $t2, $zero, 0
addi $t3, $zero, 9
add $s0, $zero, $zero • The label main identifies the
loop: lw $s1, 0($t0)
add $s0, $s0, $s1
entry point of the main function.
addi $t1, $t1, 1 • The label loop identifies the
addi $t0, $t0, 4 target of the branch instruction.
bne $t1, $t3, loop
sw $s0, 0($t0) • The label intA identifies the
location of the array in data
.data 0x10010000 memory.
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 18
Programming in Assembly
• MIPS Assembly Language:
.text • Pseudo-instruction
.globl main
main: la $t0, intA • Load address (la)
add $t1, $zero, $zero
• Loads address of var. into reg.
addi $t2, $zero, 0
addi $t3, $zero, 9
add $s0, $zero, $zero
• Loads starting (or base) address
loop: lw $s1, 0($t0)
add $s0, $s0, $s1 of array intA into register $t0.
addi $t1, $t1, 1 • Array is located in data memory.
addi $t0, $t0, 4
bne $t1, $t3, loop
sw $s0, 0($t0)
.data 0x10010000
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 19
Programming in Assembly
• MIPS Assembly Language:
.text
.globl main
main: la $t0, intA
add $t1, $zero, $zero • Variable initialization.
addi $t2, $zero, 0
addi $t3, $zero, 9
add $s0, $zero, $zero
loop: lw $s1, 0($t0)
add $s0, $s0, $s1
addi $t1, $t1, 1
addi $t0, $t0, 4
bne $t1, $t3, loop
sw $s0, 0($t0)
.data 0x10010000
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 20
Programming in Assembly
• MIPS Assembly Language:
.text • Load Word (lw)
.globl main
main: la $t0, intA • Read 32-bit integer from memory.
add $t1, $zero, $zero
• EA = R[$t0] + 0
addi $t2, $zero, 0
addi $t3, $zero, 9 • EA = effective address of array
add $s0, $zero, $zero element in data memory.
loop: lw $s1, 0($t0) • $t0 = base register
add $s0, $s0, $s1
• R[$t0] = contents of $t0
addi $t1, $t1, 1
addi $t0, $t0, 4 • 0 = offset (immediate value)
bne $t1, $t3, loop • $s1 = destination register.
sw $s0, 0($t0)
.data 0x10010000
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 21
Programming in Assembly
• MIPS Assembly Language:
.text
.globl main
main: la $t0, intA
add $t1, $zero, $zero
addi $t2, $zero, 0
addi $t3, $zero, 9
add $s0, $zero, $zero
loop: lw $s1, 0($t0)
add $s0, $s0, $s1 • sum = sum + intA[index]
addi $t1, $t1, 1
addi $t0, $t0, 4
bne $t1, $t3, loop
sw $s0, 0($t0)
.data 0x10010000
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 22
Programming in Assembly
• MIPS Assembly Language:
.text
.globl main
main: la $t0, intA
add $t1, $zero, $zero
addi $t2, $zero, 0
addi $t3, $zero, 9
add $s0, $zero, $zero
loop: lw $s1, 0($t0)
add $s0, $s0, $s1
addi $t1, $t1, 1 • index = index + 1
addi $t0, $t0, 4
bne $t1, $t3, loop
sw $s0, 0($t0)
.data 0x10010000
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 23
Programming in Assembly
• MIPS Assembly Language:
.text
.globl main
main: la $t0, intA
add $t1, $zero, $zero
addi $t2, $zero, 0
addi $t3, $zero, 9
add $s0, $zero, $zero
loop: lw $s1, 0($t0)
add $s0, $s0, $s1
addi $t1, $t1, 1
addi $t0, $t0, 4 • Array offset incremented by 4.
bne $t1, $t3, loop • MIPS is byte-addressable.
sw $s0, 0($t0)
• Each word contains 4 bytes.
.data 0x10010000
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 24
Programming in Assembly
• MIPS Assembly Language:
.text • Branch on Not Equal (bne)
.globl main
main: la $t0, intA • Branch to loop if $t1 != 9.
add $t1, $zero, $zero
• Else, continue with next
addi $t2, $zero, 0
addi $t3, $zero, 9 instruction.
add $s0, $zero, $zero
loop: lw $s1, 0($t0)
add $s0, $s0, $s1 • loop is a label.
addi $t1, $t1, 1 • Identifies the instruction to
addi $t0, $t0, 4 branch to.
bne $t1, $t3, loop
• Corresponds to an address in
sw $s0, 0($t0)
program memory.
.data 0x10010000 • Branch target address.
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 25
Programming in Assembly
• MIPS Assembly Language:
.text • Store Word (sw)
.globl main
main: la $t0, intA • Write 32-bit integer to memory.
add $t1, $zero, $zero
• EA = R[$t0] + 0
addi $t2, $zero, 0
addi $t3, $zero, 9 • EA = effective address of array
add $s0, $zero, $zero element in data memory.
loop: lw $s1, 0($t0) • $t0 = base register
add $s0, $s0, $s1
• R[$t0] = contents of $t0
addi $t1, $t1, 1
addi $t0, $t0, 4 • 0 = offset (immediate value)
bne $t1, $t3, loop • $s0 = source register.
sw $s0, 0($t0)
.data 0x10010000
intA: .word 11, 22, 33, .. , 0
Fall 2020 ECE 445 - Computer Organization 26
ANOTHER SIMPLE ASSEMBLY
LANGUAGE PROGRAM
Example: Processing a string.
Fall 2020 ECE 445 - Computer Organization 27
Programming in Assembly
• Problem: Determine the length of a C-style string.
• A C-style string is a null-terminated character array.
• The null character is represented by the ASCII value 0.
• C code:
int main ( )
{
char str[];
int length = 0;
while ( str[length] != 0 )
{
length = length + 1;
}
}
Fall 2020 ECE 445 - Computer Organization 28
Programming in Assembly
• MIPS Assembly Language:
.text
.globl main
main: la $t0, str # starting address of string
add $t1, $zero, $zero # length (and index)
add $t2, $zero, $zero # address of character
lbu $s0, 0($t0) # read first character
beq $s0, $zero, end # check if null
loop: addi $t1, $t1, 1 # increment length
add $t2, $t0, $t1 # calculate address of next char.
lbu $s0, 0($t2) # read next character
bne $s0, $zero, loop # continue if not null
end: nop
.data 0x10010000
str: .asciiz “This is another null-terminated string.”
Fall 2020 ECE 445 - Computer Organization 29
ASSEMBLY LANGUAGE
PROGRAMS
Assembler directives
Assembly instructions
Pseudo-instructions (specifically, load address)
Fall 2020 ECE 445 - Computer Organization 30
Assembly Language Programs
• An assembly language program includes the following:
• Assembly language instructions
• Defined in the processor Instruction Set (Architecture)
• Human-readable form of instructions executed by the processor.
• Composed of mnemonics, register names, and labels.
• Converted to machine language instructions by the Assembler.
• Pseudo-instructions
• Included in instruction set to provide additional flexibility and functionality.
• Not executed by the processor.
• Assembler translates pseudo-instructions into real instructions.
• Assembler Directives
• Instructions (i.e. directions) to the Assembler.
• Not executed by the processor
Fall 2020 ECE 445 - Computer Organization 31
Assembler Directives
• Assembler directives allow the programmer to:
• Assemble code and data into specified sections
• Reserve space in (program) memory for uninitialized variables
• Control the appearance of program listings
• Initialize (variables in) memory
• Assemble conditional blocks
• Define global variables
• Specify libraries from which the assembler can obtain macros
• Examine symbolic debugging information
Fall 2020 ECE 445 - Computer Organization 32
Assembler Directives
Mnemonic Description
.data Defines the data segment.
Contains static data and global variables.
Directs assembler to store data and variables in data memory.
Specifies the starting address of the data segment.
.text Defines the text (program) segment.
Contains executable instructions.
Directs assembler to store instructions in program memory.
.globl main Makes the symbol main visible to other programs.
Makes it external; can be referenced from other files.
This directive must be included in an assembly language program.
Fall 2020 ECE 445 - Computer Organization 33
Assembler Directives
Mnemonic Description
.byte Allocates one or more consecutive bytes in memory.
Initializes each byte.
Specifies a comma-delimited list of 8-bit values.
.word Allocates one or more consecutive words in memory.
Initializes each word.
Specifies a comma-delimited list of 32-bit values.
.ascii Allocates one or more consecutive bytes in memory.
Stores the characters of a string in the allocated memory.
.asciiz Allocates one or more consecutive bytes in memory.
Stores the characters of a string in the allocated memory.
Appends the null character to the end of the string.
Fall 2020 ECE 445 - Computer Organization 34
Assembler Directives: Examples
.data 0x10010000
x: .byte 0x5A
y: .half 0x1234
z: .word 0x78563412
arrayA: .word 1024, 65536, 72, 4197, 247896380
arrayB: .byte 255, 126, 83, 49, 27, 203, 134
arrayC: .byte ‘A’, ‘B’, ‘C’, ‘a’, ‘b’, ‘c’
str: .ascii “This is a string.”
strz: .asciiz “This is a null-terminated string.”
Fall 2020 ECE 445 - Computer Organization 35
Pseudo-instruction (la)
• la = load address
• Not an instruction in the MIPS instruction set architecture.
• Used to load the address of a variable into a register.
• Assembler replaces the pseudo-instruction with a set of
instructions that are defined in the MIPS ISA.
Fall 2020 ECE 445 - Computer Organization 36
INTRODUCTION TO MARS
MIPS Assembler and Runtime Simulator
Fall 2020 ECE 445 - Computer Organization 37
Introduction to MARS
• MIPS Assembler and Runtime Simulator
• A lightweight Interactive Development Environment (IDE) for
programming in MIPS Assembly Language.
• Intended for educational-level use with Patterson and
Hennessey’s Computer Organization and Design.
• Can be downloaded (for free) from:
• [Link]
• Documentation can be found at the MARS website:
• [Link]
• All MIPS assembly language coding assignments must be
completed using MARS.
Fall 2020 ECE 445 - Computer Organization 38
Editing a MIPS AL Program
Text editor
Registers
Message window
Fall 2020 ECE 445 - Computer Organization 39
Assembling a MIPS AL Program
Assemble
Fall 2020 ECE 445 - Computer Organization 40
Viewing the MIPS ML Program
Executable Code
.text
Program Data
.data
Registers
Message window
Fall 2020 ECE 445 - Computer Organization 41
Running the MIPS ML Program
Run
Fall 2020 ECE 445 - Computer Organization 42
Setting Breakpoints in Code
Set breakpoint
Fall 2020 ECE 445 - Computer Organization 43
Single-stepping through Code
Run – Single-step
Fall 2020 ECE 445 - Computer Organization 44
SPIM
• An introduction to SPIM is provided in Appendix A.9 of text.
• SPIM documentation is posted on Blackboard.
• Additional resources can be found online.
Fall 2020 ECE 445 - Computer Organization 45
Questions?