svět, kde se rodí nová budoucnost   
 
Delphi headDelphi Tips
 
Hi / Lo order byte

 
How do you extract the high or low order byte from a word?  How do you insert it?  There are built in methods hi() and lo() for extracting, but for those that want to know how to do it on their own, here it is in its most efficient form. The functions for inserting bytes are not in Delphi. Note: Assembler functions return the contents of the AX register.


function GetHiByte(w: word): byte; assembler;
asm
   mov ax, w
   shr ax, 8
end;

function GetLoByte(w: word): byte; assembler;
asm
   mov ax, w
end;

function SetHiByte(b: byte; w: word): word; assembler;
asm
   xor ax, ax
   mov ax, w
   mov ah, b
end;

function SetLoByte(b: byte; w: word): word; assembler;
asm
   xor ax, ax
   mov ax, w
   mov al, b
end;


Another way of doing it:  How about REAL FAST, without using assembler (i.e. let the compiler do the work for you)???

Type
  TWord2Byte = record
                 Lo,Hi: Byte;
               end;

var W : Word;
    B : Byte;
begin
  W := $1234;
  B := TWord2Byte(W).Hi;
  writeln(TWord2Byte(W).Hi);
  { going back }
  TWord2Byte(W).Lo := $67;
  TWord2Byte(W).Hi := $98; { no shl needed! }
end.

 

Back to Index of Tips

 

 

Send mail to radek.novak@infojet.cz with questions or comments about this web site.
Copyright © 1999-2002 Infojet.cz
Last modified: 27.07.2002