Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26

Thread: Problems when switching BP7.0 real mode to protected mode

  1. #11

    Problems when switching BP7.0 real mode to protected mode

    I recommend to check this. It is not unlikely that the problem is here.

    According to the documentation, if you do word+integer, TP converts both to longint before doing the +, then, before doing the assignment, checks if the result is in [0..65535], and then converts back to word.

  2. #12

    Problems when switching BP7.0 real mode to protected mode

    I now understand the problem. According to the TP manual, random is declared like:

    function random(range:word):word;

    Therefore, the result of "20-random(40)" is considered to be word, and has to be >=0. However when random(40)>20 then this isn't the case, and you get an arithmetic underflow.

    If you change your code to:

    x:=(x+20)-random(40);

    ... it should work.

  3. #13

    Problems when switching BP7.0 real mode to protected mode

    Hey, that actually works now! Great!

    I've also been able to find some range checking errors, so now I'm finally getting somewhere!

    I'm just not completely sure I understand what the problem was... Is it that if you create a arithmetic operation such as the "20 - Random(40)", then the result of that operation must be the same as the result of the function used in the operation (i.e. the Random() function)? I just thought that Random() would return it's value and then that return value would be entered into the arithmetic operation like any other number without affecting it any further, but now it seems that it actually affects what kind of result I can get from the arithmetic operation. Is that understood correctly?

    Also, I'm now getting an overflow error in a part of my code used for encrypting and decrypting:

    [pascal]CONST C1 = 52845;
    C2 = 11719;


    FUNCTION Decrypt(Const S: String; Key: Word): String;
    VAR
    I: Byte;
    TempString : String;
    BEGIN
    TempString := '';
    FOR I := 1 TO Length(S) DO BEGIN
    TempString := TempString + Char(Byte(S[I]) XOR (Key SHR );
    Key := (Byte(S[I]) + Key) * C1 + C2; {<-- getting the overflow error on this line}
    END;
    Decrypt := TempString;
    END;
    [/pascal]

    I found this function on the internet (I actually believed it was downloaded from Borland!) and haven't changed it. I've tried getting the function to output the value of "(Byte(S[I]) + Key) * C1 + C2" to the screen and the result was always with the range Word.

    EDIT:
    I found some references to this function:
    http://www.bsdg.org/SWAG/ENCRYPT/0036.PAS.html
    http://www.q3.nu/trucomania/truco.cgi?100&ing
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  4. #14

    Problems when switching BP7.0 real mode to protected mode

    Encryption and hashing functions are usually written to intentionally overflow.
    It is easy to see that this function will overflow also, the C1 and C2 constants are so high that Key-variable will overflow on first iteration.
    So you should allow overflow around this function.
    This can be done by using {$Q+} and {$Q-} like this:

    Code:
    &#123;$Q-&#125;
    FUNCTION Decrypt&#40;Const S&#58; String; Key&#58; Word&#41;&#58; String; 
    ...
    end;
    &#123;$Q+&#125;
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

  5. #15

    Problems when switching BP7.0 real mode to protected mode

    Ahh, okay. Thanks!

    Just out of curiosity, why exactly are they set to intentionally overflow and how come this isn't dangerous to a program?
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  6. #16

    Problems when switching BP7.0 real mode to protected mode

    Because numerical overflow can be useful. You basically calculate the value x mod(High(word)+1).

    Code:
    var
      x &#58; word;    //16 bit unsigned value
      y &#58; longword;  //16 bit unsigned value
    begin
    //16 bit
    x&#58;=65535;   //upper limit for word
    x&#58;=x+10;   //x overflows and is now nine
    
    //32 bit version of above code
    y&#58;=65535;
    y&#58;=&#40;y+10&#41; mod 65536;  //also nine thanks to mod operation
    Hmm... I'm not very good at explaining this. But you see that the 16-bit calculation is simpler and possibly faster.
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

  7. #17

    Problems when switching BP7.0 real mode to protected mode

    Ahh, okay. I understand. I was just under the impression that an overflow would cause all kinds of weird stuff, but it actually just makes the variable "start over"?
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  8. #18

    Problems when switching BP7.0 real mode to protected mode

    That depends on the cpu word size. If the datatype that overflows matches the cpu word size it starts over. If it doesn't match you get an illegal value.

  9. #19

    Problems when switching BP7.0 real mode to protected mode

    Quote Originally Posted by PP2005
    Hey, that actually works now! Great!
    I'm just not completely sure I understand what the problem was... Is it that if you create a arithmetic operation such as the "20 - Random(40)", then the result of that operation must be the same as the result of the function used in the operation (i.e. the Random() function)?
    If you use a binary operator on an integer type, the compiler decides the common type between the left and right type, that is the smallest type that can handle both the left and right operand. I.e. the common type of "0..99" and "byte" is 0..255. The common type of "integer" and "word" is -32768..65535.

    Before doing the operation, the compiler converts both operands to the common type. Then the compiler does the operation.

    Assume random(40) returns 35. On the left side there is a constant, and on the right side there is a word. The smallest type that can handle both 20 and a word is 0..65535. So, 20 is "converted" to 0..65535, and 35 is "converted" to 0..65535. Then the compiler subtracts: 20-35=-15. -15 is not in the range of the common type 0..65535, therefore, you have an arithmetic overflow.

  10. #20

    Problems when switching BP7.0 real mode to protected mode

    Okay, I get it now. Thanks for the explanation!

    I'm getting my program to work as well as learning a bunch of new stuff... this is great!
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •