08 Assembly Examples
08 Assembly Examples
08 Assembly Examples
Example Programs
CSE 2312
Computer Organization and Assembly Language Programming
Vassilis Athitsos
University of Texas at Arlington
1
Overview
• We are now ready to look at several types of ARM-7
instructions.
• The goal is not to cover every single instruction and feature.
• The goal is to learn enough instructions and see enough
examples to be able to write some interesting code.
2
Hello World in Assembly
.globl _start mov r1, #32 @''
_start: str r1,[r0]
ldr r0,=0x101f1000 mov r1, #119 @ 'w'
@ ASCII codes stored str r1,[r0]
@ at [r0] get printed mov r1, #111 @ 'o'
mov r1, #104 @ 'h' str r1,[r0]
str r1,[r0] mov r1, #114 @ 'r'
mov r1, #101 @ 'e' str r1,[r0]
str r1,[r0] mov r1, #108 @ 'l'
mov r1, #108 @ 'l' str r1,[r0]
str r1,[r0] mov r1, #100 @ 'd'
mov r1, #108 @ 'l' str r1,[r0]
str r1,[r0]
mov r1, #111 @ 'o' my_exit: @do infinite loop at the end
str r1,[r0] b my_exit
3
Hello World in Assembly, Version 2.
.globl _start mov r1, #' ' @''
_start: str r1,[r0]
ldr r0,=0x101f1000 mov r1, #'w' @ 'w'
@ ASCII codes stored str r1,[r0]
@ at [r0] get printed mov r1, #'o' @ 'o'
mov r1, #'h' @ 'h' str r1,[r0]
str r1,[r0] mov r1, #'r' @ 'r'
mov r1, #'e' @ 'e' str r1,[r0]
str r1,[r0] mov r1, #'l' @ 'l'
mov r1, #'l' @ 'l' str r1,[r0]
str r1,[r0] mov r1,#'d' @ 'd'
mov r1, #'l' @ 'l' str r1,[r0]
str r1,[r0]
mov r1, #'o' @ 'o' my_exit: @do infinite loop at the end
str r1,[r0] b my_exit
4
Hexadecimal Numbers
• Hexadecimal numbers are numbers written using base-16
representation.
• Note: we use the assembler format for numbers.
– We put # before a number.
• Example:
– #123 is (123)10, i.e., 123 in decimal.
– #0x7b is (7b)16, i.e., number 7b in hexadecimal.
– #123 = #0x7b
• In the assembler environment we use, a lot of times it is much
easier to use hexadecimal values.
– You will see plenty of examples today.
• Thus, it is good to do a bit of a review in advance.
5
Hexadecimal Examples
• How do we write these numbers in hex?
– Hex is short for hexadecimal.
• #5 = #0x???
• #9 = #0x???
• #10 = #0x???
• #11 = #0x???
• #12 = #0x???
• #13 = #0x???
• #14 = #0x???
• #15 = #0x???
• #16 = #0x???
• #17 = #0x???
6
Hexadecimal Examples
• How do we write these numbers in hex?
– Hex is short for hexadecimal.
• #5 = #0x5
• #9 = #0x9
• #10 = #0xa (or #0xA)
• #11 = #0xb (or #0xB)
• #12 = #0xc (or #0xC)
• #13 = #0xd (or #0xD)
• #14 = #0xe (or #0xE)
• #15 = #0xf (or #0xF)
• #16 = #0x10
• #17 = #0x11
7
Hexadecimal Examples
• How do we write these numbers in hex?
– Hex is short for hexadecimal.
• #20 = #0x???
• #25 = #0x???
• #26 = #0x???
• #31 = #0x???
• #32 = #0x???
• #33 = #0x???
• #100 = #0x???
• #1000 = #0x???
8
Hexadecimal Examples
• How do we write these numbers in hex?
– Hex is short for hexadecimal.
• #20 = #0x14
• #25 = #0x19
• #26 = #0x1a (or #0x1A)
• #31 = #0x1f (or #0x1F)
• #32 = #0x20
• #33 = #0x21
• #100 = #0x64 Why? Because 100 = 6*16 + 4.
• #1000 = #0x3e8 (or #0x3E8)
Why? Because 1000 = 3*162 + 14*161 + 8.
9
Hexadecimal Examples
• How do we write these hex numbers in decimal?
• #0x8 = ???
• #0xa = ???
• #0xd = ???
• #0xf = ???
• #0x40 = ???
• #0xa0 = ???
• #0xd3 = ???
• #0xe4a = ???
10
Hexadecimal Examples
• How do we write these hex numbers in decimal?
• #0x8 = #8
• #0xa = #10
• #0xd = #13
• #0xf = #15
• #0x40 = #64 64 = 4*161 + 0*160
• #0xa0 = #160 160 = 10*161 + 0*160
• #0xd3 = #211 211 = 13*161 + 3*160
• #0xe4a = #3658 14*162 + 4*161 + 10*160
11
Fun with Hexadecimals
• 0xdeadbeef = ???
12
Fun with Hexadecimals
• 0xdeadbeef = 3735928559.
• This was (and is) a popular code for printing out an
error, in cheap and small LED-based hexadecimal
displays (that perhaps can only print out a
characters).
13
Conversion Tool: Google
• Try these searches on Google:
– 0xe4a to decimal
– 2014 in hex
14
Printing Some Numbers
.globl _start
_start:
ldr r4,=0x101f1000 @ r4 := 0x 101f 1000.
@ Any ASCII code stored on r4 gets printed
mov r0, #1
add r0, r0, #48
str r0, [r4]
• What does this program do?
15
Printing Some Numbers
.globl _start
_start:
ldr r4,=0x101f1000 @ r4 := 0x 101f 1000.
@ Any ASCII code stored on r4 gets printed
mov r0, #1
add r0, r0, #48
str r0, [r4]
• What does this program do?
– It prints "1".
16
Printing Some Numbers
.globl _start
_start:
ldr r4,=0x101f1000 @ r4 := 0x 101f 1000.
@ Any ASCII code stored on r4 gets printed
mov r0, #1
add r0, r0, 48
str r0, [r4]
• What does this program do?
17
Printing Some Numbers
.globl _start
_start:
ldr r4,=0x101f1000 @ r4 := 0x 101f 1000.
@ Any ASCII code stored on r4 gets printed
mov r0, #1
add r0, r0, 48
str r0, [r4]
• What does this program do?
– It does not compile (48 should be #48).
– If you type "make", you get:
– test1.s:7: Error: shift expression expected -- `add r0,r0,48' 18
Printing Some Numbers
.globl _start
_start:
ldr r4,=0x101f1000 @ r4 := 0x 101f 1000.
@ Any ASCII code stored on r4 gets printed
mov r0, #1
add r0, r0, #48
str r0, [r4]
• How do we modify this program to print "2" instead
of "1"?
19
Printing Some Numbers
.globl _start
_start:
ldr r4,=0x101f1000 @ r4 := 0x 101f 1000.
@ Any ASCII code stored on r4 gets printed
mov r0, #2
add r0, r0, #48
str r0, [r4]
• How do we modify this program to print "2" instead
of "1"?
20
Printing Some Numbers
.globl _start
_start:
ldr r4,=0x101f1000 @ r4 := 0x 101f 1000.
@ Any ASCII code stored on r4 gets printed
mov r0, #2
add r0, r0, #48
str r0, [r4]
• How do we modify this program to print numbers
from 2 to 8?
21
.globl _start mov r0, #5
_start: add r0, r0, #48
ldr r4,=0x101f1000 str r0, [r4]
@ ASCII codes stored
@ at [r4] get printed mov r0, #6
add r0, r0, #48
mov r0, #2 str r0, [r4]
add r0, r0, #48
str r0, [r4] mov r0, #7
add r0, r0, #48
mov r0, #3 str r0, [r4]
add r0, r0, #48
str r0, [r4] mov r0, #8
add r0, r0, #48
mov r0, #4 str r0, [r4]
add r0, r0, #48
str r0, [r4] my_exit: @do infinite loop at the end
b my_exit 22
Printing Numbers
2 to 8 .globl _start
_start:
ldr r4,=0x101f1000
• To print numbers @ ASCII codes stored
@ at [r4] get printed
from 2 to 8, it makes
sense to use a loop. mov r0, #2
• Notice: my_loop:
– labels cmp r0, #8
bgt my_exit
– cmp
add r1, r0, #48
– bgt str r1, [r4]
add r0, r0, #1
b my_loop
print? my_loop:
0123456789ABCDEF cmp r0, #0xf
bgt my_exit
• The code on the
cmp r0, #10
right prints what addlt r1, r0, #48
we want. addge r1, r0, #55
str r1, [r4]
add r0, r0, #1
b my_loop
25
.globl _start lsr r1, r0, #0
_start: and r1, r1, #0x0000000f
ldr r4,=0x101f1000 cmp r1, #10
@ ASCII codes stored addlt r1, r1, #48
@ at [r4] get printed addge r1, r1, #55
str r1, [r4]
mov r0, #0x98
mov r1, #13
my_loop: str r1, [r4]
cmp r0, #0xA5 mov r1, #10
bgt my_exit str r1, [r4]
.globl _start
_start:
ldr r4, =0x101f1000
@ ASCII codes stored
@ at [r4] get printed
27
Printing a 32-bit Number in Hex
• Step 2: store some number to r0.
• Note: the mov instruction does not allow us to use arbitrary
32-bit constants, we can only use 8-bit constants. (Why?)
@ set r0 := 0x12ad730f
mov r0, #0x12
lsl r0, r0, #8
add r0, r0, #0xad
lsl r0, r0, #8
add r0, r0, #0x73
lsl r0, r0, #8
add r0, r0, #0x0f
28
Printing a 32-bit Number in Hex
• Step 2, shorter (but not faster) version: store some number to
r0.
• Use the ldr pseudoinstruction
@ set r0 := 0x12ad730f
ldr r0, =0x12ad730f
29
Printing a 32-bit Number in Hex
mov r2, #28
• Step 3: Print each digit, my_loop:
using a loop. cmp r2, #0
• For each of the 8 digits, blt my_exit
starting from the leftmost lsr r1, r0, r2
digit: and r1, r1, #0x0000000f
– Shift bits to the right, so that cmp r1, #10
the digit becomes the
addlt r1, r1, #48
rightmost.
addge r1, r1, #55
– Isolate that digit by taking a
bitwise AND with 0x0000000f. str r1, [r4]
– Print the digit. sub r2, r2, #4
b my_loop
my_exit: @do infinite loop at the end
b my_exit
30
Printing a 32-bit Number in Hex
mov r2, #28
• For example: r0 := 0x12ad730f
my_loop:
• First (most significant) digit: 1.
cmp r2, #0
• By how many bits do we need to
blt my_exit
shift to make this digit rightmost?
??? bits. lsr r1, r0, r2
• Register r2 holds the number of and r1, r1, #0x0000000f
bits we need to shift. cmp r1, #10
• We do the shift. addlt r1, r1, #48
– Note that we store the result addge r1, r1, #55
on another register, not r0.
str r1, [r4]
– We still need the rest of the
data on r0. sub r2, r2, #4
• Result: r1 := ??? b my_loop
my_exit: @do infinite loop at the end
b my_exit
31
Printing a 32-bit Number in Hex
mov r2, #28
• For example: r0 := 0x12ad730f
my_loop:
• First (most significant) digit: 1.
cmp r2, #0
• By how many bits do we need to
blt my_exit
shift to make this digit rightmost?
28 bits. lsr r1, r0, r2
• Register r2 holds the number of and r1, r1, #0x0000000f
bits we need to shift. cmp r1, #10
• We do the shift. addlt r1, r1, #48
• Result: r1 := 0x00000001. addge r1, r1, #55
• Now, we do bitwise AND between str r1, [r4]
r1 and 0x0000000f.
• Result: 0x00000002. sub r2, r2, #4
• We have managed to isolate the b my_loop
digit, ready to print it. my_exit: @do infinite loop at the end
b my_exit
32
Printing a 32-bit Number in Hex
mov r2, #28
• example: r0 := 0x12ad730f
my_loop:
• Second digit: 2.
cmp r2, #0
• By how many bits do we need to
blt my_exit
shift to make this digit rightmost?
??? bits. lsr r1, r0, r2
• Register r2 := r2 - 4, to hold the and r1, r1, #0x0000000f
number of bits we need to shift. cmp r1, #10
• We do the shift. addlt r1, r1, #48
• Result: r1 := ??? addge r1, r1, #55
• Now, we do bitwise AND between str r1, [r4]
r1 and 0x0000000f.
• Result: ???. sub r2, r2, #4
b my_loop
my_exit: @do infinite loop at the end
b my_exit
33
Printing a 32-bit Number in Hex
mov r2, #28
• example: r0 := 0x12ad730f
my_loop:
• Second digit: 2.
cmp r2, #0
• By how many bits do we need to
blt my_exit
shift to make this digit rightmost?
24 bits. lsr r1, r0, r2
• Register r2 := r2 - 4, to hold the and r1, r1, #0x0000000f
number of bits we need to shift. cmp r1, #10
• We do the shift. addlt r1, r1, #48
• Result: r1 := 0x00000012 addge r1, r1, #55
• Now, we do bitwise AND between str r1, [r4]
r1 and 0x0000000f.
• Result: 0x00000002. sub r2, r2, #4
• We have managed to isolate the b my_loop
digit, ready to print it. my_exit: @do infinite loop at the end
b my_exit
34
Printing a 32-bit Number in Hex
mov r2, #28
• example: r0 := 0x12ad730f
my_loop:
• Third digit: a.
cmp r2, #0
• By how many bits do we need to
blt my_exit
shift to make this digit rightmost?
??? bits. lsr r1, r0, r2
• Register r2 := r2 - 4, to hold the and r1, r1, #0x0000000f
number of bits we need to shift. cmp r1, #10
• We do the shift. addlt r1, r1, #48
• Result: r1 := ??? addge r1, r1, #55
• Now, we do bitwise AND between str r1, [r4]
r1 and 0x0000000f.
• Result: ??? sub r2, r2, #4
b my_loop
my_exit: @do infinite loop at the end
b my_exit
35
Printing a 32-bit Number in Hex
mov r2, #28
• example: r0 := 0x12ad730f
my_loop:
• Third digit: a.
cmp r2, #0
• By how many bits do we need to
blt my_exit
shift to make this digit rightmost?
20 bits. lsr r1, r0, r2
• Register r2 := r2 - 4, to hold the and r1, r1, #0x0000000f
number of bits we need to shift. cmp r1, #10
• We do the shift. addlt r1, r1, #48
• Result: r1 := 0x0000012a addge r1, r1, #55
• Now, we do bitwise AND between str r1, [r4]
r1 and 0x0000000f.
• Result: 0x0000000a. sub r2, r2, #4
• We have managed to isolate the b my_loop
digit, ready to print it. my_exit: @do infinite loop at the end
b my_exit
36
Infinite Loops
• Why do we always put an infinite loop at the end?
• Otherwise, some programs keep rerunning from the
beginning.
• I have verified that even correct code can get into
this behavior.
• This phenomenon is somewhat complicated to
explain, but it is easy to fix.
37
Infinite Loops
• First of all, how to fix:
Short version:
• At the very end of your program (every program that
you write), put these lines (or something equivalent):
the_end:
b the_end
39
Infinite Loops
• The assembly programs that we write run in a very
primitive environment.
• How does a program know when to stop?
• What does the CPU execute when the program
stops?
40
Infinite Loops
• The assembly programs that we write run in a very
primitive environment.
• How does a program know when to stop?
• What does the CPU execute when the program
stops?
• These are issues that are typically handled by an
operating system.
• In our case, the the program runs on a simulated
machine with no operating system.
• When the program finishes, what is the CPU
supposed to do?
41
Infinite Loops
• When the program finishes, what is the CPU supposed to do?
• The CPU just fetches the next instruction from memory.
• What is the next instruction?
• It is just whatever happened to reside in memory at that time.
• Thus, while you think that your program has finished
executing, the program still executes meaningless
instructions.
• However, at some point, the program may reach memory that
you have used on your stack.
• Some of the data you have stored on the stack, when
interpreted as instructions, executes a branch to the
beginning of the program.
42
Infinite Loops
• In summary: correct code getting into an infinite loop is a
problem that you may or may have not run across.
• If you have not run across it,do not worry about it.
• If you have, it may take hours trying to find the mistake where
there isn't one.
• Using the suggested fixes (especially the one that prints END
at the end) resolves this issue.
• Plus, using the suggested fixes ensures that if you do observe
an infinite loop, the problem is with your code.
• If you mess up your stack (by adding or subtracting the wrong
values, or restoring the value of lr from the wrong place) you
may get all sorts of weird execution behavior.
43