arithmetic operations
DESCRIPTION
Arithmetic Operations. Arithmetic Operations. Signed/unsigned integer 64-bit 96-bit (extended precision) Floating-point 32-bit (single precision) 64-bit (double precision) 128-bit (quadraple precision) 64-bit logical operations. Integer Operations. Signed Integer - PowerPoint PPT PresentationTRANSCRIPT
Natawut Nupairoj Assembly Language 1
Arithmetic Operations
Natawut Nupairoj Assembly Language 2
Arithmetic Operations
• Signed/unsigned integer– 64-bit– 96-bit (extended precision)
• Floating-point– 32-bit (single precision)– 64-bit (double precision)– 128-bit (quadraple precision)
• 64-bit logical operations
Natawut Nupairoj Assembly Language 3
Integer Operations
• Signed Integer– [-2(n-1), 2(n-1)-1]
• Unsigned Integer– [0, 2n-1]
• Indentical Operations: add, sub, ...• Difference in how to interpret condition codes
and branches• add, addcc*, sub, subcc*• Format: add rs, reg_or_imm, rd
Natawut Nupairoj Assembly Language 4
Condition Codes
• V - set when the register is not long enough to hold the result.
• N - set when the most significant bit is 1.• C - set when the operation generates a carry.• Z - set when all bits of the result are zero.
Natawut Nupairoj Assembly Language 5
Constants Representations
• Integermov 97, %r1 ! Decimal number
mov 0141, %r1 ! Octal number
mov 0x61, %r1 ! Hexadecimal number
• Charactermov 'a', %r1
mov "a", %r1
• ASCII table
Natawut Nupairoj Assembly Language 6
Integer Example
do {
x--; /* x = x - 1; */
} while(x != 0)
• Approach#1loop:
sub %l0, 1, %l0
cmp %l0, 0
bne loop
nop
• Approach#2loop:
subcc %l0, 1, %l0
bnz loop
nop
Natawut Nupairoj Assembly Language 7
Logical Operations
• and, andn, xor, or, xnor, orn• andce, andncc, xorcc, orcc, xnorcc, orncc• not
• a andn b = a and (not b)• a xnor b = a xor (not b)• a orn b = a or (not b)
Natawut Nupairoj Assembly Language 8
Logical Example
SPARC Logical 0 0 1 1 a
Instr. Operations 0 1 0 1 b
and a and b 0 0 0 1
andn a and (not b) 0 0 1 0
or a or b 0 1 1 1
orn a or (not b) 1 0 1 1
xor a xor b 0 1 1 0
xnor a xor (not b) 1 0 0 1
Natawut Nupairoj Assembly Language 9
Shift Operations
• There are 3 shift instructions:
0
srl
sra
0
sll
Natawut Nupairoj Assembly Language 10
Shift Examples
mov 1, %l1 ! %l1 = 00 00 00 00000001
sll %l1, 4, %l2 ! %l2 = 00 00 00 00010000
srl %l2, %l1, %l3 ! %l3 = 00 00 00 00001000
sll %l1, 31, %l2 ! %l2 = 10000000 00 00 00
sra %l2, 3, %l3 ! %l3 = 11110000 00 00 00
srl %l2, 3, %l3 ! %l3 = 00010000 00 00 00
Natawut Nupairoj Assembly Language 11
Our Third Program
• Convert pack decimal number in “x” (8 digits, no sign) to be stored in “y” in binary format.
• Example:– convert pack-decimal “12345678” to binary.Pack-decimal:– 0001 0010 0011 0100 0101 0110 0111 1000Binary:– 0000 0000 1011 1100 0110 0001 0100 1110
– two approaches from left to right or from right to left.– We will do from right to left.
Natawut Nupairoj Assembly Language 12
Our Third Program
• First, extract the rightmost digit.• Then, multiply it with 10^(digit-1) and add it to
the result.• Shift x to right 4 times to get the next digit.• Repeat until all digits are done.• For example: to convert “12345678”.8*10^(1-1) + 7*10^(2-1) + … + 1*10^(8-1) = 12345678
• We will need a variable to keep track the value to multiply to each digit.
Natawut Nupairoj Assembly Language 13
Our Third Program
int main()
{
int x, y, num, i, mulval;
x = 0x12345678; // number to be converted.
y = 0; // result.
mulval = 1; // 10^(digit-1).
for(i=0 ; i < 8 ; i++) {
num = x & 0xF; // extract the rightmost digit.
y = y + num*mulval; // add to the result.
x = x >> 4; // next digit.
mulval = mulval * 10;
}
}
Natawut Nupairoj Assembly Language 14
Our Third Program
define(x_r, l0)
define(y_r, l1)
define(i_r, l2)
define(mulval_r, l3)
define(num_r, l4)
.global main
main: save %sp, -64, %sp
set 0x12345678, %x_r !load 32-bit constant to x
clr %y_r ! y = 0;
mov 1, %mulval_r ! mulval = 1;
Natawut Nupairoj Assembly Language 15
Our Third Program
! Convert for to while loop
clr %i_r ! i = 0;
loop: cmp %i_r, 8 ! if i >= 8
bge done ! then exit the loop
nop ! delay slot
and %x_r, 0xF, %num_r ! num = x & 0xF;
mov %num_r, %o0
mov %mulval_r, %o1
call .mul ! num * mulval
nop ! delay slot
add %y_r, %o0, %y_r ! y = y + num*mulval;
srl %x_r, 4, %x_r ! x = x >> 4;
Natawut Nupairoj Assembly Language 16
Our Third Program
mov %mulval_r, %o0
mov 10, %o1
call .mul ! mulval*10
nop ! delay slot
mov %o0, %mulval_r ! mulval = mulval*10;
add %i_r, 1, %i_r ! i++;
ba loop ! repeat loop
nop ! delay slot
done: mov 1, %g1 ! end of program
ta 0
Natawut Nupairoj Assembly Language 17
Synthetic Instructions using %g0
• "cmp" is actually a synthetic instruction.– This instruction is not existed !!!– But it got translated to something else !!!
• cmp %r1, 12 = subcc %r1, 12, %g0• For example:
– to compare %r1 and 12, first sub %r1 with 12.– if result = 0, %r1 = 12. (Z = 1)– If result < 0, %r1 < 12. (N = 1)– If result > 0, %r1 > 12. (N = 0)
Natawut Nupairoj Assembly Language 18
Comparison and Condition Codes
• Instructionbebneblbgblebge
• Condition CodesZ = 1Z = 0(N xor V) = 1(N xor V) = 0(Z or (N xor V)) = 1(Z or (N xor V)) = 0
Natawut Nupairoj Assembly Language 19
Other Synthetic Instructions
• mov 201, %o2 = or %g0, 201, %o2• mov %g5, %i6 = or %g0, %g5, %g6• clr %i7 = or %g0, %g0, %i7• tst %l6 = subcc %l6, %g0, %g0
Natawut Nupairoj Assembly Language 20
Set 32-bit Constant to Register
set 0x12345678, %l2
• For any instruction, the biggest (or smallest) constant value is 4095 (or -4096) which is a 22-bit constant. (Why?)
• For 32-bit constant, we use the “set” instruction which is a synthetic instruction.
• It is converted to:sethi %hi(0x12345678), %l2
or %l2, %lo(0x12345678), %l2
Natawut Nupairoj Assembly Language 21
Set 32-bit Constant to Register
• 0001 0010 0011 0100 0101 0110 0111 1000
sethi %hi(0x12345678), %l2
• Store the first 22-bit of the constant to %l2.
or %l2, %lo(0x12345678), %l2
• Store the last 10-bit of the constant to %l2.
Done by assembler*