home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH01 / SIGNEXT / SIGNX.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1995-11-04  |  6.8 KB  |  244 lines

  1. (*****************************************************************************)
  2. (*                                                                           *)
  3. (* SignExt                                                                   *)
  4. (*                                                                           *)
  5. (* 11/4/95                                                                  *)
  6. (* Randall L. Hyde                                                           *)
  7. (* Copyright 1995, All Rights Reserved Unless Otherwise Noted                *)
  8. (*                                                                           *)
  9. (* This program lets the user enter an eight-bit binary or hexadecimal value.*)
  10. (* It then zero and sign extends this value to 16 bits and displays the res- *)
  11. (* ults in binary and hex.                             *)
  12. (*                                                                           *)
  13. (* Runs under Windows 3.1, Windows 95, and Windows NT.                       *)
  14. (* Source Code: Borland Delphi (object Pascal).                              *)
  15. (*                                                                           *)
  16. (*****************************************************************************)
  17.  
  18.  
  19.  
  20. unit Signx;
  21.  
  22. interface
  23.  
  24. uses
  25.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  26.   Forms, Dialogs, StdCtrls, ExtCtrls,
  27.  
  28.   { Special unit that provides bin <-> dec <-> hex conversions.    }
  29.  
  30.   Converts;
  31.  
  32. type
  33.   TSignExtend = class(TForm)
  34.  
  35.     { Data entry boxes for the form: }
  36.  
  37.     BinEntry1: TEdit;
  38.     HexEntry1: TEdit;
  39.  
  40.     { The results are written to the following strings on the form: }
  41.  
  42.     SignExtBin: TLabel;
  43.     SignExtHex: TLabel;
  44.     ZeroExtBin: TLabel;
  45.     ZeroExtHex: TLabel;
  46.  
  47.     { Labels and other artifacts appearing on the form: }
  48.     DivLine: TPanel;
  49.     MainLbl: TLabel;
  50.     SExtLbl: TLabel;
  51.     ZExtLbl: TLabel;
  52.     DataEntryLbl: TLabel;
  53.  
  54.     { Buttons and various methods for this form: }
  55.  
  56.     ExitBtn: TButton;
  57.     AboutBtn: TButton;
  58.  
  59.     procedure ExitBtnClick(Sender:TObject);
  60.     procedure AboutBtnClick(Sender:TObject);
  61.     procedure BinEntry1KeyUp(Sender:TObject; var Key:Word; Shift:TShiftState);
  62.     procedure HexEntry1KeyUp(Sender:TObject; var Key:Word; Shift:TShiftState);
  63.     procedure FormClick(Sender:TObject);
  64.     procedure FormCreate(Sender:TObject);
  65.  
  66.   private
  67.   public
  68.     value1:integer;
  69.   end;
  70.  
  71. var
  72.   SignExtend: TSignExtend;
  73.  
  74. implementation
  75.  
  76. {$R *.DFM}
  77.  
  78.  
  79.  
  80.  
  81. { DoCalc-                                }
  82. { This procedure does the sign and zero extension operations.  The pro-    }
  83. { gram calls this procedure whenever a value in one of the input text    }
  84. { boxes changes.  It updates the zero extension and sign extension    }
  85. { output labels on the form.                        }
  86.  
  87. procedure DoCalc;
  88. begin
  89.  
  90.   with SignExtend do begin
  91.  
  92.      { First, check to see if we've got a positive or negative 8-bit    }
  93.      { value.                                }
  94.  
  95.      if ((Value1 and $80) <> 0) then begin
  96.  
  97.          { Okay, we've got a negative value.  OR in $FF into the H.O.    }
  98.         { byte for sign extension, zero out the H.O. eight bits for    }
  99.         { zero extension.  Then  output the results to the appropriate    }
  100.         { strings on the form.                        }
  101.  
  102.         SignExtBin.Caption := IntToBin(Value1 or $FF00, 16);
  103.         SignExtHex.Caption := IntToHex(Value1 or $FF00, 4);
  104.         ZeroExtBin.Caption := IntToBin(Value1 and $FF, 16);
  105.         ZeroExtHex.Caption := IntToHex(Value1 and $FF, 4);
  106.  
  107.      end
  108.      else begin
  109.  
  110.          { If the input value is positive, just zero extend everything.    }
  111.  
  112.         SignExtBin.Caption := IntToBin(Value1 and $FF,16);
  113.         SignExtHex.Caption := IntToHex(Value1 and $FF, 4);
  114.         ZeroExtBin.Caption := SignExtBin.Caption;
  115.         ZeroExtHex.Caption := SignExtHex.Caption;
  116.  
  117.      end;
  118.  
  119.   end;
  120.  
  121. end;
  122.  
  123.  
  124.  
  125.  
  126.  
  127. { The program calls the following procedure when the user presses the    }
  128. { QUIT button.                                }
  129.  
  130. procedure TSignExtend.ExitBtnClick(Sender: TObject);
  131. begin
  132.      Halt;
  133. end;
  134.  
  135.  
  136. { HexEntry1KeyUp:                            }
  137. {                                    }
  138. { The program calls the following procedure whenever the users presses    }
  139. { and releases a key in the hexadecimal data entry box.  This procedure    }
  140. { checks the input to make sure it is still a valid hexadecimal value    }
  141. { and (if it is) then it computes the zero and sign extension values.    }
  142. { If the input is invalid, this procedure changes the background color    }
  143. { to red, signifying an illegal value.                    }
  144.  
  145. procedure TSignExtend.HexEntry1KeyUp(Sender: TObject;
  146.                                  var Key: Word;
  147.                                  Shift: TShiftState);
  148. begin
  149.  
  150.      { See if this is a valid hex string }
  151.  
  152.      if (CheckHex(HexEntry1.Text)) then begin
  153.  
  154.      { If valid, convert it to an integer, update the binary input    }
  155.         { text box, and do the appropriate calculations.        }
  156.  
  157.         Value1 := HexToInt(HexEntry1.Text);
  158.         BinEntry1.Text := IntToBin(Value1,8);
  159.         HexEntry1.Color := clWindow;
  160.         BinEntry1.Color := clWindow;
  161.         DoCalc;
  162.  
  163.      end
  164.      else begin
  165.  
  166.            { If the input was invalid, beep the speaker and set the back-}
  167.           { ground color to red.                    }
  168.  
  169.           MessageBeep($ffff);
  170.           HexEntry1.Color := clRed;
  171.  
  172.      end;
  173.  
  174. end;
  175.  
  176.  
  177. {  BinEntry1KeyUp-                            }
  178. {                                    }
  179. { This procedure is essentially the same as the one above, except it    }
  180. { handles data entry in the binary text entry box.            }
  181. { See the comments in the procedure above for details.            }
  182.  
  183. procedure TSignExtend.BinEntry1KeyUp(    Sender:TObject;
  184.                     var Key:Word;
  185.                                         Shift:TShiftState
  186.                      );
  187. begin
  188.  
  189.      if (CheckBin(BinEntry1.Text)) then begin
  190.  
  191.         Value1 := BinToInt(BinEntry1.Text);
  192.         HexEntry1.Text :=  IntToHex(Value1,2);
  193.         BinEntry1.Color := clWindow;
  194.         HexEntry1.Color := clWindow;
  195.         DoCalc;
  196.  
  197.      end
  198.      else begin
  199.  
  200.           MessageBeep($ffff);
  201.           BinEntry1.Color := clRed;
  202.  
  203.      end;
  204.  
  205. end;
  206.  
  207.  
  208. { The following procedure displays the "About Box" whenever the user    }
  209. { presses the "ABOUT" button.                        }
  210.  
  211. procedure TSignExtend.AboutBtnClick(Sender: TObject);
  212. begin
  213.  
  214.     MessageDlg(
  215.        'Sign and Zero Extension Operations, Copyright 1995 by Randall Hyde',
  216.        mtInformation, [mbOk], 0);
  217.  
  218. end;
  219.  
  220.  
  221.  
  222. { FormClick:                                }
  223. { This procedure cleans up the data in the data entry text boxes when-    }
  224. { ever the user clicks on the background form.                }
  225.  
  226. procedure TSignExtend.FormClick(Sender: TObject);
  227. begin
  228.  
  229.      HexEntry1.text := IntToHex(value1,2);
  230.      BinEntry1.text := IntToBin(value1,8);
  231.  
  232. end;
  233.  
  234. { This procedure executes whenever the program first starts running.    }
  235. { It handles any necessary initialization.                }
  236.  
  237. procedure TSignExtend.FormCreate(Sender: TObject);
  238. begin
  239.  
  240.      Value1 := 0;
  241. end;
  242.  
  243. end.
  244.