Chapter 3: Introduction To Assembly Language Programming: CEG2400 - Microcomputer Systems
Chapter 3: Introduction To Assembly Language Programming: CEG2400 - Microcomputer Systems
Stack Reg.
Link Reg.
Program counter
operands
label opcode The objects to be operate by opcode comment
9
Ceg2400 Ch3 assembly V.6b
Assemble Instruction
• One line of code - first : ADD r1,r2,r3
optional
opcode
Bit 31 Bit 0
MSB=0 , the number is +ve.
MSB=1 , the number is –ve.
32-bit data
Overflow Value1 + value2 > +2,147,483,647
Range of If the result is above the line, it is overflowed.
valid value
7FFF FFFF Hex=
-Value2
+2,147,483,647
-Value1
0
-Value3
-Value4
8000 0000 Hex=
-2,147,483,648 Underflow
-Value3 - value4 < -2,147,483,648
Ceg2400 Ch3 assembly V.6b 20
The general format of an assembly
instruction
• All instructions have this form:
• op{cond}{S} Rd, Rn, Operand2
ADDS
ADDS r0,
r0,r1,
r1,r2
r2 ;;r0
r0:=
:=r1
r1++r2r2
ADCS
ADCS r0,
r0,r1,
r1,r2
r2 ;;r0
r0:=
:=r1
r1++r2r2++CC
SUBS
SUBS r0,
r0,r1,
r1,r2
r2 ;;r0
r0:=
:=r1
r1--r2
r2
SBCS
SBCS r0,
r0,r1,
r1,r2
r2 ;;r0
r0:=
:=r1
r1--r2
r2++CC--11
If you add the ‘s’ suffix to an op-code, the instruction will affect the CPSR
(N,Z,C,V flags)
e.g.
• ADD r0, r1, r2 ; r0 := r1 + r2, CPSR (NZCV flags will not be affected)
• ADDS r0, r1, r2 ; r0 := r1 + r2, CPSR (NZCV flags will be affected)
•
PC PC (Hex) C R0(Hex) R1(Hex) R2 (Hex)
All registers R0-R2 are rest to 0 here 0 0 0 0
0000 1000 MOV r1,#15 ;r1=15 0000 1004 0 0000 0000 0000 000f 0
R1=55H=0101 0101 B
R2=61H=0110 0001 B
9EH=1001 1110 B
MOV
MOV r0,
r0,r2
r2 ;;r0
r0:=
:=r2
r2
MVN
MVN r0,
r0,r2
r2 ;;r0
r0:=
:=not
notr2
r2
CMP
CMP r1,
r1,r2
r2 ;;set
setcondition
conditioncode
codeon
onr1
r1--r2
r2(compare)
(compare)
•
0000 1000 MOV r1,#0x11 ;r1=0000 0011
B V alue represented
0010 2 2
0011 3 3
Sign and
b3 b2b1b0 magnitude 1' s complement 2' s complement 0100 4 4
0101 5 5
0 1 1 1 +7 +7 + 7
0 1 1 0 +6 +6 + 6 0110 6 6
0 1 0 1 +5 +5 + 5
+4 +4 + 4
0111 7 7
0 1 0 0
0 0 1 1 +3 +3 + 3 1000 8 8
0 0 1 0 +2 +2 + 2
0 0 0 1 +1 +1 + 1 1001 9 9
0 0 0 0 +0 +0 + 0 1010 10 A
1 0 0 0 - 0 -7 - 8
1 0 0 1 - 1 -6 - 7 1011 11 B
1 0 1 0 - 2 -5 - 6
1100 12 C
1 0 1 1 - 3 -4 - 5
1 1 0 0 - 4 -3 - 4 1101 13 D
1 1 0 1 - 5 -2 - 3
- 6 - 1 - 2
1110 14 E
1 1 1 0
1 1 1 1 - 7 -0 - 1 1111 15 F
Ceg2400 Ch3 assembly V.6b 44
Addition (1-bit)
0 1 0 1
+ 0 + 0 + 1 + 1
0 1 1 10
Carry-out
0 0000
1111 0001
N- 1 1
N-2 1110 0010
2 -1 0 +1
- 2 +2
1101 0011
- 3 +3
1100 -4 +4 0100
-5 +5
1011 0101
- 6 +6
- 7 - 8 +7
1010 0110
1001 0111
1000
(a) Circle representation of integers mod N (b) Mod 16 system for 2's-complement numbers
• Overflow
– When two +ve numbers are added (MSB is 0) , the
result is –ve (MSB is 1)
• Underflow
– When two -ve numbers are added (MSB is 1) , the
result is +ve (MSB is 0)
• Note:
– MSB is the most significant bit
– In 2’s complement representation MSB is the sign
bit (see appendix)
Ceg2400 Ch3 assembly V.6b 52
Overflow
The result is too big for the bits
• In 2’s complement arithmetic
– addition of opposite sign numbers never overflow
– If the numbers are the same sign and the result is the opposite
sign, overflow has occurred (Range is -2n-1 to 2n-1-1). Usually CPU
overflow status bit will be setup and use software to deal with
it.
– E.g. 0111+0100=1011 (but 1011 is -5)
– 7 + 4= 12 (too large to be inside the 4-bit 2’s)
– Because 4-BIT 2’S complement range is only -23 to 23-1
– Or -8 to 7
• Previous examples are small numbers. In our usual programs they are
bigger.
• What is the range for a signed char type -- -- char (8-bit number)?
• What is the range for a signed integer type -- int32 (32-bit number)?
• What will you do if the result is overflowed?
• Answer: sign extension, see previous slides, e.g., turn a 4-bit number to 8-bit
etc.
• Positive number – add 0’s to LHS
– e.g. 0111 -> 00000111
• Negative number – add 1’s to LHS
– e.g. 1010 ->11111010
• &
• The ampersand (&) is used to denote hexadecimal.
Thus,
• 0xF00D
• hF00D
• F00Dh
• $F00D (see later comment on the use of $) &F00D are all
identical, but using different ways to denote base 16. We
shall be using the &F00D notion.