Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Use the Stack! Say we have an HLL function: (int à 16 bit, char à 8 bits) Local Variables myfunc(char arg1, char arg2) { char locvar1; char locvar2; How many variables? ...... } Local variables in a subroutine must be created before execution and destroyed upon exit. Week 5: Local Variables - Floating Point Numbers 1 3 Assembly code for myfunc() Local Variables myfunc: • In a HLL, local variables are created when a function is called, and destroyed when it returns. • HLLs allow recursion, so a single function can have several sets of local variables simultaneously – one for each call. • What can we do about this? Week 5: Local Variables - Floating Point Numbers Week 5: Local Variables - Floating Point Numbers 2 (put the 2 values on stack, subtract 1(char) from SP for each) ..... body of function ..... (add 2 (for 2-chars) to SP) RTS Week 5: Local Variables - Floating Point Numbers 4 Local Variables on the Stack index-mode address 7(SP) ..... 6(SP) arg1 5(SP) arg2 4(SP) Return address 3(SP) Return address 2(SP) locvar2 1(SP) locvar1 0(SP) ...... The Stack Frame • Many compilers (and some programmers) dedicate an address register other than the SP for referring to data on the stack frame. This register is known as the frame pointer (FP) • The advantage of this is largely organizational: the FP remains constant as the function executes, whereas the SP varies as data gets pushed and popped SP before local variables added Space for local variables SP after local variables added Week 5: Local Variables - Floating Point Numbers 5 Week 5: Local Variables - Floating Point Numbers Frame Pointer Usage Stack Frames MYSUB • The complete set of data on the stack associated with a single function call is called a stack frame • So the diagram on the previous slide represents one complete stack frame PSHX TSX * Save old value of IX * Transfer SP to IX * IX is now the Frame Pointer * use IX to reach the stack content PULX RTS Week 5: Local Variables - Floating Point Numbers 7 6 * Restore IX Week 5: Local Variables - Floating Point Numbers 8 Example (stack content) Call-by-Value Example After the execution of the code DFFB segment. Stack content inside DFFC the procedure ADD. DFFD • Write a subroutine that adds two numbers which are passed via stack using call-byvalue technique. • The return value must be in Accumulator A. • Use X as the frame pointer in the subroutine. • Subroutine does not destroy X register content (i.e. IX is saved at the beginning and restored at the exit) Week 5: Local Variables - Floating Point Numbers DFFE DFFF MAIN MAIN push values to stack restore stack EXIT ORG FCB FCB RMB $C200 $32 $11 1 Data Section 1st number to add 2nd number to add Allocate memory for SUM ORG LDS LDAA PSHA LDAA PSHA JSR STAA TSX LDAB ABX TXS SWI $C000 #$DFFF NUM1 start address of the main program Initialize the Stack pointer Read 1st number Push it onto stack Read 2nd number Push it onto stack Jump to Subroutine ADD Return value is in A Store it in SUM Clean up the stack We have pushed 2 bytes onto stack add 2 to X restore SP to its original value return to monitor program NUM2 ADD SUM #2 Week 5: Local Variables - Floating Point Numbers ret. addr. (H) ret. addr. (L) num2 num1 #$DFFF NUM1 NUM2 ADD 9 Week 5: Local Variables - Floating Point Numbers Example (main program) NUM1 NUM2 SUM LDS LDAA PSHA LDAA PSHA JSR SP 11 Example (subroutine) SP DFFB DFFC DFFD DFFE ret. addr. (H) ret. addr. (L) num2 DFFF num1 X (after executing TSX) LDAA 2,X LDAA 3,X We destroy X register content by TSX instruction. We need to save it somewhere. Where?? à STACK (PSHX) 10 Week 5: Local Variables - Floating Point Numbers 12 Example (main program) Example (subroutine) ADD ORG PSHX TSX LDAA ADDA PULX RTS $C100 4,X 5,X Start address of the subroutine Save the original IX into stack IX ß SP+1 to access parameters Read NUM2 into A Add NUM to A, The result is in A Pull the original IX from stack Return from subroutine NUM1 NUM2 SUM MAIN push addresses onto stack DFF9 DFFA DFFB DFFC DFFD DFFE DFFF SP IX(H) IX (L) ret. addr. (H) ret. addr. (L) num2 num1 IX restore stack 4,X 5,X Week 5: Local Variables - Floating Point Numbers EXIT $C200 $32 $11 1 Data Section 1st number to add 2nd number to add Allocate memory for the result ORG LDS LDX PSHX LDX PSHX LDX PSHX JSR TSX LDAB ABX TXS SWI $C000 #$DFFF #NUM1 start address of the main program Initialize the Stack pointer X ß address of 1st number Push it onto stack X ß address of 2nd number Push it onto stack X ß address of the result Push it onto stack Jump to Subroutine ADD2 Clean up the stack We have pushed 6 bytes onto stack add 6 to X restore SP to its original value return to monitor program 13 #NUM2 #SUM ADD2 #6 15 Week 5: Local Variables - Floating Point Numbers Example (stack content) Call-by-Reference Example After the execution of the code segment. Stack content at the beginning of the subroutine ADD2 • Write a subroutine that adds two numbers which are passed via stack using call-byreference technique. • The return value must be in Accumulator A. • Use X as the frame pointer in the subroutine. • Subroutine does not destroy IX and IY registers content (i.e. They are saved at the beginning and restored at the exit) Week 5: Local Variables - Floating Point Numbers ORG FCB FCB RMB MAIN 14 LDS LDX PSHX LDX PSHX LDX PSHX JSR SP DFF7 DFF8 ret. addr. (H) DFF9 ret. addr. (L) DFFA sum addr. (H) DFFB sum addr. (L) DFFC num2 addr. (H) #$DFFF #NUM1 DFFD num2 addr. (L) DFFE num1 addr. (H) #NUM2 DFFF num1 addr. (L) #SUM ADD2 we need to PSHX and PSHY at the beginning of the subroutine Week 5: Local Variables - Floating Point Numbers 16 Stack Content PSHX and PSHY DFF4 DFF5 DFF6 DFF7 DFF8 DFF9 DFFA DFFB DFFC DFFD DFFE DFFF IY (H) IY (L) IX (H) IX (L) ret. addr. (H) ret. addr. (L) sum addr. (H) sum addr. (L) num2 addr. (H) num2 addr. (L) num1 addr. (H) num1 addr. (L) IX after TSX LDY STAA 6,X 0,Y LDY LDAA 8,X 0,Y LDY LDAA 10,X 0,Y This is how to reach values whose addresses are in the stack. We will assume that X points to IY (H). Week 5: Local Variables - Floating Point Numbers Floating Point Numbers Representation of Real Numbers in Microprocessors 17 Subroutine ADD2 ADD2 ORG $C100 PSHX PSHY PSHA TSX LDY 11,X LDAA 0,Y CLR 0,Y LDY 9,X ADDA 0,Y CLR 0,Y LDY 7,X STAA 0,Y PULA PULY PULX RTS 19 Floating Point Numbers Start address of the subroutine Save the original value of IX into stack Save the original value of IY into stack Save the original value of A into stack IX ß SP+1 to access parameters Read Address of NUM1 into IY Read NUM1 into A (IY points to NUM1) Clear NUM1 Read Address of NUM2 into IY Add NUM2 to A, The result is in A Clear NUM2 Read Address of SUM into IY Write the sum into SUM Pull the original A from stack Pull the original IY from stack Pull the original IX from stack Return from subroutine Week 5: Local Variables - Floating Point Numbers Week 5: Local Variables - Floating Point Numbers • Registers for real numbers usually contain 32 or 64 bits, allowing 2^32 or 2^64 numbers to be represented. • There are an infinite number between 2 adjacent integers. (or two reals!!) • Which bit patterns for reals selected? • We have to use the scientific notation 18 Week 5: Local Variables - Floating Point Numbers 20 Changing the Behavior of Bits Integers We can group the functions of bits. We can approximate any real number with three groups. Each bit has a function. We put the meaning on each. 10101111 20 21 22 23 24 25 26 27 S: E: f: Weighted Sum (-1)S Week 5: Local Variables - Floating Point Numbers 21 Changing the Meaning of Bits I order to be able to represent real numbers in binary, we need to change the behavior of each bit. 2 -3 2 -2 2 -1 20 21 22 23 24 2E Week 5: Local Variables - Floating Point Numbers 23 • How many bits are reserved for S, E and F? It depends on the standart. • The most widely used standart is IEEE 754 Floating poing standart. • Another standart, especially for 8 or 16 bit microprocessors is the SSS (Small System Standart) Standart. Weighted Sum Week 5: Local Variables - Floating Point Numbers f Floating Point Numbers Put a decimal point and change the function of each bit. 10101 111 Sign (one bit is enough) Exponent Mantissa 22 Week 5: Local Variables - Floating Point Numbers 24 Increasing Resolution Normalization • We have limited number of bits assigned to mantissa. i.e. we can "approximate" real numbers not exactly represent them. • So, how can we maximize the number of digits we can obtain? i.e. how can we increase the resolution? • This is the question of where to put the "point" sign? • Normalization makes sure that the most accurate representation of any real number is obtained. • If any operation (subtraction, addition, etc) yields a number of which does not have a 1 next right to the point, it must be normalized. • Exponent adjusted to obtain a 1 right next to the point. Week 5: Local Variables - Floating Point Numbers 25 0.0000101010101011 can be adjusted as: 0.1010101010110000 x 2-4 Representation: S Make sure that the digit right next to the point sign is a 1. Adjust the exponent. E F 32 bits •S is one bit representing the sign of the number •E is an 8 bit biased integer representing the exponent •F is an unsigned integer The true value represented is: (-1)S x f x 2e • S = sign bit • e = E – bias • f = F/2n + 1 • for single precision numbers n=23, bias=127 more room for more detail Week 5: Local Variables - Floating Point Numbers 27 IEEE 754 Single Precision Floating Point Format Increasing Resolution • We can adjust the exponent value to increase the efficiency in the mantissa. Week 5: Local Variables - Floating Point Numbers 26 Week 5: Local Variables - Floating Point Numbers 28 IEEE 754 Single Precision Floating Point Format How to convert 64.2 into IEEE SP 1. Get a binary representation for 64.2 • Binary of left of radix pointis: • Binary of right of radix S, E, F all represent fields within a representation. Each is just a bunch of bits. .2 x 2 = 0.4 .4 x 2 = 0.8 .8 x 2 = 1.6 .6 x 2 = 1.2 S is the sign bit • (-1)S à (-1)0 = +1 and (-1)1 = -1 • Just a sign bit for signed magnitude • • E is the exponent field • The E field is a biased-127 representation. • True exponent is (E – bias) • The base (radix) is always 2 (implied). • Some early machines used radix 4 or 16 (IBM) Week 5: Local Variables - Floating Point Numbers Binary for .2: 64.2 is: 2. Normalize binary form • Produces: 29 IEEE 754 Single Precision Floating Point Format Week 5: Local Variables - Floating Point Numbers 31 Floating Point Numbers 3. Turn true exponent into bias-127 F (or M) is the fractional or mantissa field. 4. Put it together: 23-bit F is: S E F is: In hex: • It is in a strange form. •f = F/2n + 1 • There are 23 bits for F. • A normalized FP number always has a leading 1. • No need to store the one, just assume it. • This MSB is called the HIDDEN BIT. Week 5: Local Variables - Floating Point Numbers 0 0 1 1 •Since floating point numbers are always stored in normal form, how do we represent 0? • 0x0000 0000 and 0x8000 0000 represent 0. • What numbers cannot be represented because of this? 30 Week 5: Local Variables - Floating Point Numbers 32 IEEE Floating Point Format IEEE Floating Point Format Other special values: • +5/0=+ •+ = 0 11111111 00000… (0x7f80 0000) • -7/0 = •= 1 11111111 00000… (0xff80 0000) • 0/0 or + += NaN (Not a number) • NaN ? 11111111 ?????… (S is either 0 or 1, E=0xff, and F is anything but all zeroes) • Also de-normalized numbers (beyond scope) Week 5: Local Variables - Floating Point Numbers What is 47.62510 in SP FP format? 33 IEEE Floating Point Format Week 5: Local Variables - Floating Point Numbers SSS (Small System Standard) What is the decimal value for this SP FP number 0x4228 0000? EXP S MANTISSA 1bit 8 bits 2s complement signed binary (-1)S Week 5: Local Variables - Floating Point Numbers 35 34 15 bits binary fraction F 2EXP Week 5: Local Variables - Floating Point Numbers 36 Notes on SSS Conversion of FP to Decimal • Mantissa is always an even number since mantissa is 15 bits. We assume the right most 16th bit is a 0. • Mantissa is always between $8000 and $FFFE since the most significant bit (right next to the point) is a 1. • Mantissa is between 0.5 ($0.8000) and 0.9999694 ($0.FFFE) Week 5: Local Variables - Floating Point Numbers • Method 1: Convert hex to decimal (make sure sign bit is handled appropriately), divide by 32768 and multiply it by 2EXP • Method 2: Shift mantissa left or right by exponent. 37 39 Float to Integer Conversion of a number to FP Returns the decimal number in ACCD. F2INT • Step 1: if the number > 1 then divide it by 2, increment exponent each time until number is in the range [0.5, 1) • Step 1: If the number < 0.5 then multiply by 2, decrement exponent each time until the number is in the range [0.5, 1) • Step 2: multiply mantissa with 32768, convert it to hex after rounding. Week 5: Local Variables - Floating Point Numbers Week 5: Local Variables - Floating Point Numbers START LOOP 38 ORG $C000 CLRB LDAA EXP1 BGT START CLRA RTS CLRA LSL ROL LSL ROL ROLB ROLA DEC BNE RTS check exponent if > 0 then start D ß 0 MANT1+1 MANT1 MANT1+1 MANT1 remove sign bit " " " " move mantissa into D EXP1 LOOP decrement until exponent = 0 Week 5: Local Variables - Floating Point Numbers 40 Addition / Subtraction Why Did the Ariane 5 Explode? check exponent (have to match the larger one) repeat increase smaller exponent by 1 shift mantissa right until smaller exponent matches the larger or mantissa = 0 add/subtract mantissa if add carries rotate right, increase exponent if subtract borrows shift left, decrement exponent • The cause was traced to the Inertial reference system (SRI). • Both the main and backup SRI failed. • Both units failed due to an out-of-range conversion – Input: double precision floating point – Output: 16-bit integer for “horizontal bias” (BH) • Careful analysis during software design had indicated that BH would “fit” in 16 bits • So, why didn’t it fit? Question: Why match the larger one? Think about that. Week 5: Local Variables - Floating Point Numbers 41 Why Did the Ariane 5 Explode? Week 5: Local Variables - Floating Point Numbers 43 Why did the Ariane 5 Explode? • In 1996 Ariane 5 Flight 501 exploded after launch. • Estimated cost of accident: $500 million • Careful analysis during software design had indicated that BH would “fit” in 16 bits • BUT, all analysis had been done for the Ariane 4, the predecessor of Ariane 5 - software was reused • Since Ariane 5 was a larger rocket, the values for BH were higher than anticipated • AND, there was no handler to deal with the exception! • For more information: – http://java.sun.com/people/jag/Ariane5.html Week 5: Local Variables - Floating Point Numbers 42 Week 5: Local Variables - Floating Point Numbers 44