3

The goal of the program below is to accept up to 10 signed 8-byte floating-point numbers within the range -100 ≤ X ≤ 100 as input from a user and store them into an array. The user input is received using the ReadFloat Irvine method. If a number outside that range is entered, the subroutine is supposed to stop executing and return through eax the number of values currently in the array. That was just a bit of context intended to describe what the program is supposed to do. The problem I am having with this code is that it does not loop properly after accepting the first value. I have it set up so that it checks to see if the input number is above or equal to -100 in L1, and then below or equal to 100 in L2. If the number is outside of that range, the subroutine should stop executing, but if it is within that range, it should progress to L3 & R1. In L3 and R1, the number is placed into an index in theSFPArray and if the array has less than 10 values in it, the program should unconditionally jump back to L1 for further iterations. The JMP command in R1 is where the problem is. The subroutine in its current state will stop executing after a single number is input and I can't figure out why. Can anyone provide assistance?

INCLUDE    c:\irvine\irvine32.inc
INCLUDELIB c:\irvine\irvine32.lib
INCLUDELIB c:\masm32\lib\user32.lib
INCLUDELIB c:\masm32\lib\kernel32.lib


.data
theSFPArray REAL8 10 dup(?)             ;an array that can store up to 10 signed floating point numbers
tempStoreFP REAL8 ?                     ;this variable will temporarily store the FP number acquired from user input, and then push it onto the stack
lengthOfSFPArray DWORD ?                ;this variable will store the length of theSFPArray. This value will be used to determine if requestSignedFloats should stop looping.
inputLoopCounter DWORD -1               ;used to determine when the requestSignedFloats subroutine should stop accepting input.                 
prompt BYTE "Please enter a value: ",0


.CODE

main PROC

    call    requestSignedFloats
    exit

main ENDP

requestSignedFloats PROC

    finit                                 ;initializes floating point unit
    push    edx                           ;pushes the original value of edx onto the stack. This will be popped when the subroutine ends.
    mov     edx, OFFSET theSFPArray       ;moves the offset of theSFPArray into edx so that values can be placed into it.
    push    edx                           ;pushes edx onto the stack while it contains the offset of the SFPArray for later usage.   

    mov eax,100
    push eax
    fild dword ptr [esp]                  ;get the 100 from memory and throw it onto the FPU, into ST(0)
    fchs                                  ;changes the 100 in ST(0) into -100
    pop eax                  

L1: 
    mov     edx,OFFSET prompt                          
    call    WriteString                   ;displays the String within the prompt variable on the screen.          
    call    ReadFloat                     ;requests a float as input from the user and stores it at the top of the floating point stack, aka ST(0).
    fcom                                  ;compares the value in ST(1) to the value in ST(0).
    jae     L2
    pop     edx                           ;this line and the two lines below it will execute if the comparison dictates that ST(1) is below the value in ST(0). This should cause the subroutine to end.
    pop     edx                           ;a second pop of edx is necessary to restore edx to its original value since two alterations of edx were placed onto the stack at the beginning of the subroutine.
    mov     lengthOfSFPArray,LENGTHOF theSFPArray ;Moves the current number of values stored in theSFPArray into the lengthOfSFPArray variable.
    mov     eax,lengthOfSFPArray          ;Returns in eax,the number of values in the array, as specified by the requirements
    ret
L2: 
    fstp    tempStoreFP                   ;pops the user input value off of the stack temporarily so that fchs can be used to change the sign of the value in ST(0)
    fchs                                  ;changes the -100 in ST(0) into a positive 100.
    fld     tempStoreFP                   ;pushes tempStoreFP back onto the stack so that its value is now in ST(1)
    fcom    
    jbe     L3
    pop     edx                           ;this line and the two lines below it will execute if the comparison dictates that ST(1) is below the value in ST(0). This should cause the subroutine to end.
    pop     edx                           ;a second pop of edx is necessary to restore edx to its original value since two alterations of edx were placed onto the stack at the beginning of the subroutine.
    mov     lengthOfSFPArray,LENGTHOF theSFPArray ;Moves the current number of values stored in theSFPArray into the lengthOfSFPArray variable.
    mov     eax,lengthOfSFPArray          ;Returns in eax,the number of values in the array, as specified by the requirements
    ret
L3: 
    pop     edx                           ;this is done to pop the offset of theSFPArray off of the stack and back into edx since at this point edx still stores the "prompt".
    inc     inputLoopCounter              ;increments inputLoopCounter so that its value is equal to the index that the number input by the user will be stored in.
    mov     ecx,inputLoopCounter          ;uses inputLoopCounter to determine how many times the loop will execute.
R1:
    inc     edx                           ;increments edx an amount of times equivalent to the value stored in inputLoopCounter.
    loop    R1
    fstp    qword ptr [edx]               ;takes the value at the top of the stack and stores it as a REAL8 at the address specified by edx (aka its array index)
    mov     lengthOfSFPArray,LENGTHOF theSFPArray ;Moves the current number of values stored in theSFPArray into the lengthOfSFPArray variable.
    fchs                                  ;changes the 100 in ST(0) to a -100 in preparation for the next iteration of the subroutine.
    cmp     inputLoopCounter,10
    je      L4
    jmp     L1                            ;An unconditional jump to L1 that causes this subroutine to execute repeatedly. The line above this one prevents it from being an infinite loop.
L4:
    mov     eax,lengthOfSFPArray          ;Returns in eax,the number of values in the array, as specified by the requirements
    pop     edx                           ;if the program makes it to this point, the offset of the array would have been popped off of the stack, meaning the original value of edx is the only thing
                                          ;remaining on the stack, so only one pop is necessary
    ret

requestSignedFloats ENDP
Proto
  • 99
  • 5

1 Answers1

5

In your .data section you define lengthOfSFPArray like this

lengthOfSFPArray DWORD ?                ;this variable will store the length of theSFPArray. This value will be used to determine if requestSignedFloats should stop looping.

The ? means that the initial value is undefined and therefore anything between 0 and 2^32-1.

In L1 you retrieve that undefined value with

mov     eax,lengthOfSFPArray          ;Returns in eax,the number of values in the array, as specified by the requirements

So EAX will be undefined or whatever value lengthOfSFPArray had at initialization. You repeat that in L2.

In R1 you set lengthOfSFPArray with

mov     lengthOfSFPArray,LENGTHOF theSFPArray

to the LENGTHOF of theSFPArray, defined in the data section as

theSFPArray REAL8 10 dup(?)

which is by definition the number of elements in the theSFPArray: LENGTHOF(theSFPArray) = 10.

After that, you compare the value of 10 to the value of 10, which is always TRUE:

cmp     lengthOfSFPArray,10
je      L4                     ; ALWAYS jump to L4
jmp     L1                     ; NEVER reached

L4: is your exit label, so the whole procedure is only executed once.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • Just to make sure I understand correctly, are you saying that the line: "theSFPArray REAL8 10 dup(?)" causes theSFPArray to have not just 10 indexes, but rather 10 indexes filled with 10 undefined elements, and that's the reason the line: "cmp lengthOfSFPArray,10" is always true? – Proto May 07 '16 at 19:49
  • 1
    @Proto: ` lengthOfSFPArray` is set to `LENGTHOF theSFPArray`. The `LENGTHOF` directive [Returns the number of items in array variable.](http://www.phatcode.net/res/223/files/html/Chapter_8/CH08-5.html) Because the _number of items_ in `theSFPArray REAL8 10 dup(?)` is 10 by definition `LENGTHOF theSFPArray` returns 10. And if you compare `lengthOfSFPArray` = 10 to 10, you will always get `TRUE`. The line: "theSFPArray REAL8 10 dup(?)" has exactly 10 items (and **not indexes**), but 80 bytes. `LENGTHOF` returns the number of items. To get the number of BYTEs, you would have to use `SIZEOF`. – zx485 May 07 '16 at 20:02
  • I've updated my original post with my code after the slight modifications I made that I thought would solve the issues you mentioned, but it still does not seem to work. I added lines in L1 and L2 that move the LENGTHOF theSFPArray into the lengthOfSFPArray variable. Additionally, I made the cmp line in R1 based on the inputLoopCounter variable instead of the lengthOfSFPArray variable. Is there something I'm not understanding correctly? – Proto May 07 '16 at 20:36
  • 1
    I removed yesterdays comments. Today I had a more thorough look at your code: Your stack and your loops are now functional. There should occur no problems. However, you use `FCOM` wrong. If you look [at this FPU tutorial](http://www.ray.masmcode.com/tutorial/fpuchap7.htm#fcom) you'll find that `FCOM` sets the FPU status word and not the CPU flags you refer to in your `jae` and `jbe` instructions following the compare. A possible solution would be using `FCOMI` instead which sets the CPU flags according to the compare operation. Hope this helps. – zx485 May 08 '16 at 10:01
  • After replacing the fcom lines in L1 and L2 with "fcomi ST(0),ST(1)", I tested the program, but it is behaving strangely. The program no longer stops executing after accepting the first floating point number and now requests a second one. However, the program will always crash after I input the second floating point number. I have absolutely no idea what the cause of that might be. – Proto May 08 '16 at 20:30