Assembly Language and System Software Lab Exercise. Finish all

Assembly Language and System Software Lab Exercise. Finish all the lab exercise under 50 minutes. In this lab exercise, you will learn to use the floating point unit (FPU). The FPU maintains a stack which can support up to 8 floating point numbers. Please read the text book. In FPU, there are eight registers, namely st(0), st(1), …, st(7). Build a program in Release mode and an executable file project.exe is created in “Release” folder. Run it. Before you read the program solution, you must try to implement the programs first.
p1. Ask a user to input a floating number and print it on the screen. Notice that you must read the book for more information about the floating point unit (FPU). finit ; initialize FPU mWriteln “Enter a floating point number:” call ReadFloat; mWriteln “The floating point number is:” call mWriteFloat Build and run the program. Data set one: Input: 123.5 Output:? Data set two: Input: ‐123.45123456 Output:? Data set three: Input: 123.45123456 Output:? Check the outputs to see whether or not they are the same as the inputs. Question: Why are they the same? Why not? p2. Implement a loop to read 8 floating point numbers and show the content of the FPU each time. Try to draw the stack and the content of the stack in each step. So that you will understand clearly how the stack works. The loop structure can be achieved by using ECX as the counter and then use the loop operation to jump to the starting point of the loop structure. For example: mov ecx, 10 mov eax, 0 L1: inc eax L2: loop L1 In this case, “inc eax” will be called for 10 times. Each time ecx is decreased by 1. When the value of ecx is zero at L2, the loop is exited. Therefore, after the loop eax = 10. Thus, we write the followings: finit mov ecx, 8 L1: mWriteln “Enter a floating point number:” call ReadFloat call ShowFPUStack loop L1 mWriteln ‘Press a key to quit.’ Please see which registers store the floating point numbers and how they are changed. Input Set One: 1, 2, 3, 4, 5, 6, 7, 8. Input Set Two: 8, 7, 6, 5, 4, 3, 2, 1. Part2: After that try the following: .data myFloat REAL8 ? .code …… finit mov ecx, 8 L1: mWriteln “Enter a floating point number:” call ReadFloat call ShowFPUStack fstp myFloat ; pop off a number from the stack and store it to myFloat loop L1 mWriteln “Load MyFloat to stack…” fld myFloat ; load myFloat to st(0); mWriteln “MyFloat is:” L2: call WriteFloat call ShowFPUStack Question: What is the output at Line L2? Why? Also check the content of the FPU.
p3. Ask a user to input a floating number. Then add with two other floating point numbers. Show the sum on the screen. In each time, show the FPU content. .data first REAL8 1.1 second REAL8 1.2 .code finit ; initialize FPU mWriteln “Enter a floating point number:” call ReadFloat; ; st(0) stores the floating point number f. call ShowFPUStack fld first ; load first to the stack. Thus, st(1) stores f and st(0) stores first call ShowFPUStack faddp st(1), st(0) ; s t(1) = st(1) + st(0); then st(0) is popped off, setting st(0) = st(1) Call ShowFPUStack ; load first to the stack. Thus, st(1) = st(0), st(0) = second fld second call ShowFPUStack faddp st(1), st(0) ; s t(1) = st(1) + st(0); then st(0) is popped off, setting st(0) = st(1) mWriteln “The sum is:” call mWriteFloat ; write st(0) on the screen. call ShowFPUStack Build and run the program. Here, we add “call ShowFPUStack“ to track the content of the FPU. Data set one: Input: ‐2.1 Output: ? Data set two: Input: ‐2.3 Output: ? Question: Is the result zero for Data set two? Why? Why not? p4. Compute (1/1+1/2+1/3+1/4+…+…1/n)*2, where n is a user input positive integer. Assume that n is not larger than 100. Show the result on the screen. Here, we need to use loop structures. Steps: 1. Ask the user to input n. 2. use a loop structure to store 1, 2, 3, .., and n in an array A. 3. use a loop structure to compute 1/1, ½, 1/3, …, 1/n, based on A and store them to an array B. 4. use a loop structure to add all the elements of B and store the sum to S. 5. compute S*2 and show the result. Step 0. .data n DWORD ? ; ? the value is not initialized ZERO REAL8 0.0 ONE REAL8 1.0 TWO REAL8 2.0 A REAL8 100 DUP(0.0) ; memory space for 100 REAL8s B REAL8 100 DUP(0.0) ; memory space for 100 REAL8s S REAL8 ? .code Step 1. L0: mWriteln “Please enter n:” mReadInt cmp eax, 100 jna L1 ; eax above 100?, no ‐> jump to L1 jmp L0 ; eax is above 100. Ask to input again. L1: mov n, eax Step 2. use a loop structure to store 1, 2, 3, .., and n in an array A. mov edi, 0 ; edi =0; let edi point to the first element of A finit mov ecx, n ; set the loop counter, ecx = n fld ZERO ; st(0) = 0 L2: fld ONE ; st(0) = 1 faddp st(1), st(0) ; Try to see why we can produce 1, 2, 3, 4, …… fst A[edi] ; note, edi must store the correct offset! add edi, 8 ; REAL8, 8 bytes. Let edi point to the next element loop L2 To verify that we have computed 1, 2, 3, 4, … correctly, we must print the elements of A on the screen. mWriteln “The content of A:” mov esi, 0 ; esi =0; let edi point to the first element of A mov ecx, n L3: T1: finit fld REAL8 PTR A[esi] call WriteFloat add esi, 8 loop L3 Do you see that the numbers are shown on the same line. This is too messy. Modify the code so that it shows only one number at one line. After testing Step 2, remove line T1 and run the program again. What do you see? Step 3. use a loop structure to compute 1/1, ½, 1/3, …, 1/n, based on A and store them to an array B. mov edi, 0 mov esi, 0 ; mov ecx, n L4: ; compute B[edi] = 1/A[esi] finit fld ONE fdiv REAL8 PTR A[esi]; st(0) = st(0) / A[esi] fst B[esi] add edi, 8 add esi, 8 loop L4 To verify that we have computed 1/1, 1/2, 1/3, 1/4, … correctly, we must print the elements of B on the screen. mWriteln “The content of B:” mov esi, 0 ; esi =0; let edi point to the first element of B mov ecx, n L5: finit fld REAL8 PTR B[esi] call WriteFloat add esi, 8 loop L5 Step 4. use a loop structure to add all the elements of B and store the sum to S. Then show S. finit mov ecx, n mov esi, 0 fld ZERO L6: fadd REAL8 PTR B[esi] add esi, 8 loop L6 fst S // show S finit fld S ; st(0) = S mWriteln “1/1+1/2+1/3+..+1/n:” call WriteFloat ;mWriteln “ “ Step 5. compute S*2 and then show the result. finit fmul TWO ; st(0) = st(0)*2. Note that in the previous code, st(0) = S. fst S mWriteln “(1/1+1/2+1/3+..+1/n)*2:” call WriteFloat ;mWriteln “ “ Enjoy programming.