Arithmetic Operations Natawut Nupairoj Assembly Language 1 Arithmetic
Arithmetic Operations Natawut Nupairoj Assembly Language 1
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 2
Integer Operations • Signed Integer – [-2(n-1), 2(n-1)-1] • Unsigned Integer – [0, 2 n-1] • Indentical Operations: add, sub, . . . • Difference in how to interpret condition codes and branches • add, addcc*, subcc* • Format: add rs, reg_or_imm, rd Natawut Nupairoj Assembly Language 3
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 4
Constants Representations • Integer mov mov 97, %r 1 0141, %r 1 0 x 61, %r 1 ! Decimal number ! Octal number ! Hexadecimal number • Character mov 'a', %r 1 "a", %r 1 • ASCII table Natawut Nupairoj Assembly Language 5
Integer Example do { x--; /* x = x - 1; */ } while(x != 0) • Approach#1 • Approach#2 loop: sub %l 0, 1, %l 0 cmp %l 0, 0 bne loop nop loop: subcc %l 0, 1, %l 0 bnz loop nop Natawut Nupairoj Assembly Language 6
Logical Operations • and, andn, xor, xnor, orn • andce, andncc, xorcc, xnorcc, orncc • not • a andn b • a xnor b • a orn b Natawut Nupairoj = a and (not b( = a xor (not b( = a or (not b( Assembly Language 7
Logical Example SPARC Instr. andn or orn xor xnor Natawut Nupairoj Logical Operations a and b a and (not b) a or b a or (not b) a xor b a xor (not b) 0 0 0 1 Assembly Language 0 1 0 1 0 0 1 1 1 0 1 a b 8
Shift Operations • There are 3 shift instructions: Natawut Nupairoj Assembly Language 9
Shift Examples mov sll srl sll sra srl 1, %l 1, 4, %l 2, %l 1, %l 3 %l 1, 31, %l 2, 3, %l 3 Natawut Nupairoj ! ! ! %l 1 %l 2 %l 3 Assembly Language = = = 00 00 00 10000000 11110000000100001000 00 00 10
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 1000 Binary: – 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 11
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 12
Our Third Program int main() { int x, y, num, i, mulval; x = 0 x 12345678; y = 0; mulval = 1; for(i=0 ; i < 8 ; i++) { num = x & 0 x. F; y = y + num*mulval; x = x >> 4; mulval = mulval * 10; } // number to be converted. // result. // 10^(digit-1). // extract the rightmost digit. // add to the result. // next digit. } Natawut Nupairoj Assembly Language 13
Our Third Program main: define(x_r, l 0) define(y_r, l 1) define(i_r, l 2) define(mulval_r, l 3) define(num_r, l 4). global main save %sp, -64, %sp set clr mov Natawut Nupairoj 0 x 12345678, %x_r !load 32 -bit constant to x %y_r ! y = 0; 1, %mulval_r ! mulval = 1; Assembly Language 14
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, 0 x. F, %num_r ! num = x & 0 x. F; mov %num_r, %o 0 mov %mulval_r, %o 1 call. mul ! num * mulval nop ! delay slot add %y_r, %o 0, %y_r ! y = y + num*mulval; srl %x_r, 4, %x_r ! x = x >> 4; Natawut Nupairoj Assembly Language 15
Our Third Program done: mov call nop mov add ba nop %mulval_r, %o 0 10, %o 1. mul mov ta 1, %g 1 0 Natawut Nupairoj %o 0, %mulval_r %i_r, 1, %i_r loop ! ! ! mulval*10 delay slot mulval = mulval*10; i++; repeat loop delay slot ! end of program Assembly Language 16
Synthetic Instructions using %g 0 • "cmp" is actually a synthetic instruction. – This instruction is not existed !!! – But it got translated to something else !!! • cmp %r 1, 12 = subcc %r 1, 12, %g 0 • For example: – – to compare %r 1 and 12, first sub %r 1 with 12. if result = 0, %r 1 = 12. (Z = 1) If result < 0, %r 1 < 12. (N = 1) If result > 0, %r 1 > 12. (N = 0) Natawut Nupairoj Assembly Language 17
Comparison and Condition Codes • Instruction be bne bl bg ble bge Natawut Nupairoj • Condition Codes Z=1 Z=0 (N xor V) = 1 (N xor V) = 0 (Z or (N xor V)) = 1 (Z or (N xor V)) = 0 Assembly Language 18
Other Synthetic Instructions • • mov 201, %o 2 = or %g 0, 201, %o 2 mov %g 5, %i 6 = or %g 0, %g 5, %g 6 clr %i 7 = or %g 0, %i 7 tst %l 6 = subcc %l 6, %g 0 Natawut Nupairoj Assembly Language 19
Set 32 -bit Constant to Register set 0 x 12345678, %l 2 • 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(0 x 12345678), %l 2 or %l 2, %lo(0 x 12345678), %l 2 Natawut Nupairoj Assembly Language 20
Set 32 -bit Constant to Register • 0001 0010 0011 0100 0101 0110 0111 1000 sethi %hi(0 x 12345678), %l 2 • Store the first 22 -bit of the constant to %l 2. or %l 2, %lo(0 x 12345678), %l 2 • Store the last 10 -bit of the constant to %l 2. Done by assembler* Natawut Nupairoj Assembly Language 21
- Slides: 21