0% found this document useful (0 votes)
49 views31 pages

Topic #4 - SIM ARM - Part 3

The document discusses loops in the SimARM assembly language. It provides examples of while, do-while, and for loops to increment a variable i up to 100 and double a variable x. It also provides an example of a program that divides two unsigned numbers using a loop, storing the quotient and remainder.

Uploaded by

jiachengwang0621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views31 pages

Topic #4 - SIM ARM - Part 3

The document discusses loops in the SimARM assembly language. It provides examples of while, do-while, and for loops to increment a variable i up to 100 and double a variable x. It also provides an example of a program that divides two unsigned numbers using a loop, storing the quotient and remainder.

Uploaded by

jiachengwang0621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Concordia University

Electrical & Computer Engineering

COEN 311
Computer Organization & Software
Topic #4 – Part 3
Simple Processor Example: SimARM

(by Prof. Sofiène Tahar


updated by Prof. Anjali Agarwal)
SimARM Loop Examples
Looping in SimARM
The combination of using cmp and bcc (conditional
branch) instructions allows for loops to be performed in
SimARM

There are three common types of loops:


― While loops
― Do-While/Repeat loops
― For loops
While Loops in General
In a While Loop, the condition is checked before
executing the loop’s statements

while (condition)
{
my_loop_contents();
}

Note: the condition may never be met/the loop does not run,
i.e., it is possible to not even enter the loop
While Loops in SimARM
Write a SimARM Assembly Program that
implements the following pseudocode:
x = 8;
i = 0;
while (i < 100)
{
i = i + 1;
x = x + x;
}
Remember that in a while loop, the condition is checked before
even entering the loop!
While Loops in SimARM
loop: cmp R0, R1 @ Compare i to 100
beq exit @ Exit if i = 100
bgt exit @ Exit if i > 100
add R0, R0, #1 @ i = i + 1
add R2, R2, R2 @ x = x + x
str R2, [R3] @ Update x in mem
b loop @ Loop back
exit: ...
Assume: i=0 in R0; 100 in R1; x=8 in R2; memory loc. of X in R3
While Loops in SimARM
loop: cmp R0, R1 @ Compare i to 100
beq exit @ Exit if i = 100
bgt exit @ Exit if i > 100
add R0, R0, #1 @ i = i + 1
add R2, R2, R2 @ x = x + x
str R2, [R3] @ Update x in mem
b loop @ Loop back
exit: ...
Assume: i=0 in R0; 100 in R1; x=8 in R2; memory loc. of X in R3
While Loops in SimARM
.thumb
.globl _start
_start: ldr R3, =i @ Get loc of i
ldr R0, [R3] @ Load i in R0
ldr R3, =X @ Get loc of x
ldr R2, [R3] @ Load x in R2
sub R1, R1, R1 @ Clear R1
add R1, R1, #100 @ R1=100 for cond. check
loop: cmp R0, R1 @ Compare i to 100
beq exit @ Exit if i = 100
bgt exit @ Exit if i > 100
add R0, R0, #1 @ i = i + 1
add R2, R2, R2 @ x = x + x
str R2, [R3] @ Update x in mem
b loop @ Loop back
exit: ldr R3, =i @ Get loc of i
str R0, [R3] @ Store i in memory
b .
x: .word 8
i: .word 0
Do-While Loops in General
Do-while loops where the loop is executed at least
once, and the condition is checked after each pass:

do
{
my_loop_contents();
} while (condition);
Do-While Loops in SimARM
Write a SimARM Assembly Program that implements
the following pseudocode:
x = 8;
i = 0;
do
{
i = i + 1;
x = x + x;
}(i < 100)
Remember that in a do-while loop, the loop is executed once
before the condition is checked!
Do-While Loops in SimARM
do: add R0, R0, #1 @ i = i + 1
add R2, R2, R2 @ x = x + x
str R2, [R3] @ Update x in memory
cmp R0, R1 @ Compare i to 100
beq exit @ Exit if equal
bgt exit @ Exit if i > 100
b do @ Loop back
exit: …

Assume: i=0 in R0; 100 in R1; x=8 in R2; memory loc. of X in R3


Do-While Loops in SimARM
do: add R0, R0, #1 @ i = i + 1
add R2, R2, R2 @ x = x + x
str R2, [R3] @ Update x in memory
cmp R0, R1 @ Compare i to 100
beq exit @ Exit if equal
bgt exit @ Exit if i > 100
b do @ Loop back
exit: …

Assume: i=0 in R0; 100 in R1; x=8 in R2; memory loc. of X in R3


Do-While Loops in SimARM
.thumb
.globl _start:
_start:
ldr R3, =i @ Get loc of i
ldr R0, [R3] @ Load value of i
ldr R3, =x @ Get loc of x
ldr R2, [R3] @ Load value of x
sub R1, R1, R1 @ Clear R1
add R1, R1, #100 @ R1=100 for cond. check
do: add R0, R0, #1 @ i = i + 1
add R2, R2, R2 @ x = x + x
str R2, [R3] @ Update x in memory
cmp R0, R1 @ Compare i to 100
beq exit @ Branch to exit if equal
bgt exit @ Branch to exit if i>100
b do @ Loop back otherwise
exit: ldr R3, =i @ Get loc of i
str R0, [R3] @ Store i in mem
b . @ End/infinite loop
x: .word 8
i: .word 0
For Loops in General
For loops have a variable that is initialized upon entering
• A condition involving the variable is tested before each pass
• The variable is either incremented or decremented after each
pass
for (type myvar; condition; var_operation)
{
my_loop_contents();
}

i=1

true
i <= 100 Body of loop i ++

false
For Loops in SimARM
Write a SimARM Assembly Program that implements
the following pseudocode:

x = 8;
for (i = 1 to 100)
{
x = x + x;
}
For Loops in SimARM
loop: cmp R0, R1 @ Compare i to 101
beq exit @ Exit if i = 101
add R0, R0, #1 @ i = i + 1
add R2, R2, R2 @ x = x + x
str R2, [R3] @ Update x in memory
b. loop @ Loop back
exit: …

Assume: i=1 in R0; 101 in R1; x=8 in R2; memory loc. of X in R3


For Loops in SimARM
loop: cmp R0, R1 @ Compare i to 101
beq exit @ Exit if i = 101
add R0, R0, #1 @ i = i + 1
add R2, R2, R2 @ x = x + x
str R2, [R3] @ Update x in memory
b. loop @ Loop back
exit: …

Assume: i=1 in R0; 101 in R1; x=8 in R2; memory loc. of X in R3


For Loops in SimARM
.thumb
.globl _start
_start: ldr R3, =x @ Get loc of x
ldr R2, [R3] @ load x in reg R2
sub R0, R0, R0 @ Clear R0
add R0, R0, #1 @ Initialize R0= i = 1
sub R3, R3, R3 @ Clear R3
add R3, R3, #101 @ R3=101 for cond. check
loop: cmp R0, R3 @ Compare i to 101
beq exit @ Exit if i = 101
add R0, R0, #1 @ i = i + 1
add R2, R2, R2 @ x = x + x
str R1, [R2] @ Update x in memory
b loop @ Loop back
exit: b . @ Infinite loop/end
x: .word 8
SimARM Assembly Programing
Write a SimARM assembly program to divide two unsigned
numbers using a loop.

uint n = 12;
uint divisor = 5;
uint quotient;
uint remainder;

while (n >= divisor)


{
n = n – divisor;
quotient++;
}

remainder = n;
SimARM Assembly Programing
Write a SimARM assembly program to divide two unsigned
numbers using a loop.

uint n = 13;
uint divisor = 5;
uint quotient;
uint remainder;

while (n >= divisor)


{
n = n – divisor; n = 13 – 5 = 8 → 8 – 5 = 2
quotient++; quotient = 1 → 2
}

remainder = n; remainder = 3
SimARM Assembly Program
.thumb
.globl _start
_start: ldr R0, =n @ Get loc of n
ldr R1, [R0] @ load n in R1
ldr R0, =divisor @ Get loc of divisor
ldr R2, [R0] @ Load divisor in R2
sub R3, R3, R3 @ Clear R3 for quotient
loop: cmp R1, R2 @ Compare R1 to R2
blt endloop @ Branch if R1 < R2
sub R1, R1, R2 @ N – divisor
add R3, R3, #1 @ Increment quotient
b loop @ Loop again
endloop: ldr R0, =quotient @ Get loc of quotient
str R3, [R0] @ Store quotient
ldr R0, =remainder @ Get loc of remainder
str R1, [R0] @ Store remainder
ldr R0, =n @ Get loc of n
str R1, [R0] @ Store n
b . @ End prog
n: .word 13
divisor: .word 5
quotient: .space 4
remainder: .space 4
Mission Accomplished!
SimARM Assembly Program
.thumb
.globl _start
_start: ldr R0, =n
ldr R1, [R0]
ldr R0, =divisor
ldr R2, [R0]
mov R3, #0
loop: cmp R1, R2
blt endloop
sub R1, R1, R2
add R3, R3, #1
b loop
endloop: ldr R0, =quotient
str R3, [R0]
ldr R0, =remainder
str R1, [R0]
ldr R0, =n
str R1, [R0]
b .
n: .word 13
divisor: .word 5
quotient: .space 4
remainder:.space 4

This program implements unsigned division by two


SimARM Machine/Assembly Program
Machine code Assembly code
Memory Hex (binary) code
addresses Labels Mnemonics Operands
0000 480F _start: ldr R0, =n
0002 6801 ldr R1, [R0]
0004 480F ldr R0, =divisor
0006 6802 ldr R2, [R0]
0008 1ADB sub R3, R3, R3
000a 4291 loop: cmp R1, R2
000c DB02 blt endloop
000e 1A89 sub R1, R1, R2
0010 3301 add R3, R3, #1
0012 E7FA b loop
0014 4809 endloop: ldr R0, =quotient
0016 6003 str R3, [R0]
0018 4809 ldr R0, =remainder
001a 6001 str R1, [R0]
001c 4801 ldr R0, =n
001e 6001 str R1,[R0]
0020 E7FE b .
0022 0000000D n: .word 13
0026 00000005 divisor: .word 5
002a 00000000 quotient: .space 4
002e 00000000 remainder: .space 4
SimARM Machine/Assembly Program
Machine code?? Assembly code
Memory Hex (binary) code
addresses Labels Mnemonics Operands
0000 480C _start: ldr R0, =n
0002 6801 ldr R1, [R0]
0004 480C ldr R0, =divisor
0006 6802 ldr R2, [R0]
0008 2300 sub R3, R3, R3
000a 4291 loop: cmp R1, R2
000c DC02 blt endloop
000e 1A89 sub R1, R1, R2
0010 3301 add R3, R3, #1
0012 E7FA b loop
0014 4809 endloop: ldr R0, =quotient
0016 6003 str R3, [R0]
0018 4809 ldr R0, =remainder
001a 6001 str R1, [R0]
001c 4805 ldr R0, =n
001e 6001 str R1,[R0]
0020 E7FE b .
0022 0000000C n: .word 13
0026 00000005 divisor: .word 5
002a 00000000 quotient: .space 4
002e 00000000 remainder: .space 4
SimARM Machine/Assembly Program
Machine code Assembly code??
Memory Hex (binary) code
addresses Labels Mnemonics Operands
0000 480F _start: ldr R0, =n
0002 6801 ldr R1, [R0]
0004 480F ldr R0, =divisor
0006 6802 ldr R2, [R0]
0008 1ADB mov R3, #0
000a 4291 loop: cmp R1, R2
000c DB02 bgt endloop
000e 1A89 sub R1, R1, R2
0010 3301 add R3, R3, #1
0012 E7FA b loop
0014 4809 endloop: ldr R0, =quotient
0016 6003 str R3, [R0]
0018 4809 ldr R0, =remainder
001a 6001 str R1, [R0]
001c 4801 ldr R0, =n
001e 6001 str R1,[R0]
0020 E7FE b .
0022 0000000D n: .word 12
0026 00000005 divisor: .word 5
002a 00000000 quotient: .space 4
002e 00000000 remainder: .space 4
Final Remark!
Compiled SimARM Assembly Program
Line # Mem_Add Instruct Assembly
========================================================================================================
1 .thumb
2 .globl _start
3 0000 480F _start: ldr R0, =n @ Get loc of n
4 0002 6801 ldr R1, [R0] @ Load n in R1
5 0004 480F ldr R0, =divisor @ Get loc of divisor
6 0006 6802 ldr R2, [R0] @ Load divisor in R2
7 0008 1ADB sub R3, R3, R3 @ Clear R3 for quotient
8 000a 4291 loop: cmp R1, R2 @ Compare R1 to R2
9 000c DB02 blt endloop @ Branch if less than
10 000e 1A89 sub R1, R1, R2 @ N = n - divisor
11 0010 3301 add R3, R3, #1 @ Increment quotient
12 0012 E7FA b loop @ Loop again
13 0014 4809 endloop: ldr R0, =quotient @ Get loc of quotient
14 0016 6003 str R3, [R0] @ Store quotient
15 0018 4809 ldr R0, =remainder @ Get loc of remainder
16 001a 6001 str R1, [R0] @ Store remainder
17 001c 4801 ldr R0, =n @ Get loc of n
18 001e 6001 str R1, [R0] @ Store n
19 0020 E7FE b . @ End prog
20 0022 0000000D n: .word 13
21 0026 00000005 divisor: .word 5
22 002a 00000000 quotient: .space 4
23 002e 00000000 remainder: .space 4
SimARM Machine Program

1
2
3 0000 480F 0100 1000 0000 1111
4 0002 6801 0110 1000 0000 0001
5 0004 480F 0100 1000 0000 1111
6 0006 6802 0110 1000 0000 0010
7 0008 1ADB 0001 1010 1101 1011
8 000a 4291 0100 0010 1001 0001
9 000c DB02 1101 1011 0000 0010
10 000e 1A89 0001 1010 1000 1001
11 0010 3301 0011 0011 0000 0001
12 0012 E7FA 1110 0111 1111 1010
13 0014 4809 0100 1000 0000 1001
14 0016 6003 0110 0000 0000 0011
15 0018 4809 0100 1000 0000 1001
16 001a 6001 0110 0000 0000 0001
17 001c 4801 0100 1000 0000 0001
18 001e 6001 0110 0000 0000 0001
19 0020 E7FE 1110 0111 1111 1110
20 0022 0000000D 0000 0000 0000 0000 0000 0000 0000 1101
21 0026 00000005 0000 0000 0000 0000 0000 0000 0000 0101
22 002a 00000000 0000 0000 0000 0000 0000 0000 0000 0000
23 002e 00000000 0000 0000 0000 0000 0000 0000 0000 0000
SimARM Machine Program
0000 0000 0000 0000 0100 1000 0000 1111
0000 0000 0000 0010 0110 1000 0000 0001
0000 0000 0000 0100 0100 1000 0000 1100
0000 0000 0000 0110 0110 1000 0000 0010
0000 0000 0000 1000 0001 1010 1101 1011
0000 0000 0000 1010 0100 0010 1001 0001
0000 0000 0000 1100 1101 1011 0000 0010
0000 0000 0000 1110 0001 1010 1000 1001
0000 0000 0001 0000 0011 0011 0000 0001
0000 0000 0001 0010 1110 0111 1111 1010
0000 0000 0001 0100 0100 1000 0000 1001
0000 0000 0001 0110 0110 0000 0000 0011
0000 0000 0001 1000 0100 1000 0000 1001
0000 0000 0001 1010 0110 0000 0000 0001
0000 0000 0001 1100 0100 1000 0000 0101
0000 0000 0001 1110 0110 0000 0000 0001
0000 0000 0010 0000 1110 0111 1111 1110
0000 0000 0010 0010 0000 0000 0000 0000 0000 0000 0000 1101
0000 0000 0010 0110 0000 0000 0000 0000 0000 0000 0000 0101
0000 0000 0010 1010 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0010 1110 0000 0000 0000 0000 0000 0000 0000 0000
SimARM Machine Program
0000 0000 0000 0000 0100 1000 0000 1111
0000 0000 0000 0010 0110 1000 0000 0001
0000 0000 0000 0100 0100 1000 0000 1100
0000 0000 0000 0110 0110 1000 0000 0010
0000 0000 0000 1000 0001 1010 1101 1011
0000 0000 0000 1010 0100 0010 1001 0001
0000 0000 0000 1100 1101 1011 0000 0010
0000 0000 0000 1110 0001 1010 1000 1001
0000 0000 0001 0000 0011 0011 0000 0001
0000 0000 0001 0010 1110 0111 1111 1010
0000 0000 0001 0100 0100 1000 0000 1001
0000 0000 0001 0110 0110 0000 0000 0011
0000 0000 0001 1000 0100 1000 0000 1001
0000 0000 0001 1010 0110 0000 0000 0001
0000 0000 0001 1100 0100 1000 0000 0101
0000 0000 0001 1110 0110 0000 0000 0001
0000 0000 0010 0000 1110 0111 1111 1110
0000 0000 0010 0010 0000 0000 0000 0000 0000 0000 0000 1101
0000 0000 0010 0110 0000 0000 0000 0000 0000 0000 0000 0101
0000 0000 0010 1010 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0010 1110 0000 0000 0000 0000 0000 0000 0000 0000

You might also like