NET3001 2 Asm
NET3001 2 Asm
Assembly Language
Sample Program
.byte d
.word d
.ascii “string”
.asciz “string”
.string “string”
create bytes in the data stream
.fill n,s,f
fills n times s bytes with f (s=1,2 or 4)
.space n,f
.skip n,f
fill n bytes with f
.org n
moves ahead by n bytes
Assembler Directives
.equ
.set
add entries to the symbol table
.
if you use the symbol “.” it means the current location
counter
this may come in hand in obscure situations
Assembly Instructions: Direction
data(32)
store
load Flash/Program
CPU addr(32) addr
match
move
RAM/variables
addr
match
other
addr
Getting numbers into registers
you can use a mov instruction to put a “small” bit
pattern into a register
mov r4,#12
mov r5,#0x3A000000
but you can't use a mov for a complex pattern
mov r7,#0x20001c44 // <----not allowed
.data
.word 0x328321 // memory at 0x2000.0000
.word 0x434 // at 0x2000.0004
.org 0x10 // skip ahead to 0x2000.0010
.fill 2 // empty space for the answer
Again, using labels
.data
nStudentsOnTime: .word 12
nLatecomers: .word 4
.org 0x10 // skip ahead to 0x2000.0010
nTotalStudents: .fill 2
Symbol Table
.data
delayTime: .word 5 // the msec to delay
Symbol Table
.text
.global calcCheckSum
.type calcCheckSum,%function
calcCheckSum:
ldr r11, =myString
ldr r10, =stringCount
ldr r12,[r10] // go to ram to get length
mov r6, #0 // this will accumulate
cs1:
ldrb r4,[r11] // fetch and add to csum
add r6, r4
add r11, #1 // point to next char
subs r12, #1 // decrement & test
bne cs1
ldr r10, =csum // and save the answer
strb r6, [r10]
Symbol Table Example 5
●this symbol table shows how it might look after the assembler
●the assembler builds the opcodes, but leaves empty spots where
the operands will go; the linker fills in the blanks from the symbol
table
Symbol Table Example 5
●the addresses above show how the symbol table might look after
the linker has finished; this example assumes there is no other
code/data in the project
●the address for calcCheckSum will get changed to 0x0800.0009
2's complement
flip all the bits and add 1
memorize these
0xFFFF.FFFF = -1
...FE = -2
...FD = -3
...FC = -4
...F0 = -16
and these while you're at it
0xA = 10 0xD = 13 0x14 = 20
0xB = 11 0xE = 14 0x1E = 30
0xC = 12 0xF = 15 0x50 = 80
Binary Math
unsigned signedhex number line unsigned
signed
82 82 52
103 103 67
185 -71 V B9 -128 0 127 255
82 82 52
202 -54 CA
28 C 28 1C -128 0 127 255
189 -67 BD
12 12 0C
-128 0 127 255
201 -55 C9
189 -67 BD
221 -35 DD -128 0 127 255
154 C -102 9A
189 -67 BD
133 -123 85 -128 0 127 255
66 C 66 V 42
Unsigned/Signed Math
two small numbers small +ve, small +ve
4,660=0x1234
4,660=0x1234 22,136=0x5678
30,328=0x7678 26,796=0x68AC
big +ve, big +ve
34,988=0x88AC 4,660=0x1234
two large numbers 30,328=0x7678
34,988=0x88AC
35,243=0x89AB
small -ve, small -ve
52,719=0xCDEF -292=0xFEDC
87,962=0x1579A -12,817=0xCDEF
-13,109=0x1CCCB
two very large numbers
big -ve, big -ve
65,244=0xFEDC -30,293=0x89ab
-12,817=0xCDEF
52,719=0xCDEF -43,110=0x1579A
117,963=0x1CCCB
small +ve, small -ve
4,660=0x1234
-12,817=0xCDEF
-8,157=0xE023
Why do we use 2's complement?
1's complement
just flip the bits
-2 0xFD
-1 0xFE
0 0 or 0xFF (either)
1 0x01
….
suppose we do a few math calculations
-2 + 3 → 0xFD + 0x03 = 0x01
but if we treat 0xFD as unsigned (253)...