Chapter 4

© 2000, The TRAC Foundation

Bit Manipulation and Display Primitives

 

This chapter describes the bit manipulation and display group of primitives.

 

4.1 Introduction to the Bit Manipulation Primitives

The bit manipulation primitives permit you to display and manipulate bits in any computer record.

TRAC handles all data as 8-bit octets. The TRAC processor does not modify these 8-bit octets. An octet can represent any of the 256 bit patterns. However, many of these 8-bit patterns have no screen (or printer) display values. Normally, only a 7-bit subset of the full range of octets is used; this is the 7-bit ASCII subset, in which the eighth bit is set to zero.

Binary data of any kind (program code of graphics) is represented inside the computer with strings of 8-bit octets. The bit manipulation primitives display and manipulate these octets. The primitives convert an octet into a viewable ASCII string. Such an ASCII string can be rearranged by a TRAC script. Then, after rearrangement, the ASCII string can be converted back to the octet format.

For example, with these primitives you can modify the binary code of an executable file on disk. You could use the file read primitive to find a specific binary segment in the file. You could then copy the segment into a TRAC form as binary octets, which are not displayable. You could convert the octets of the segment to an ASCII displayable form and modify the segment as desired. You could then convert the modified segment back to a segment of octets. Finally, using the file position and file write primitives, you could write the modified segment into the file on disk.

You can choose between three ASCII modes of representation of the octets: binary, hex, and decimal groups. For each of these modes, there is a primitive to convert from octet into the mode, and a primitive to convert back to the octet format. In addition to these six primitives, there are four primitives that provide logical operations on the individual bits of octets. The bit manipulation primitives are:

bbo Convert binary to octet.

bla Bitwise AND.

bln Negation or complement.

blo Bitwise OR.

blx Exclusive OR.

bob Convert octet to binary.

bon Convert an octet group to decimal.

box Convert octet to hex.

bxo Convert hex to octet.

You can see a list of these primitives online with the :(b?) primitive statement.

 

4.2 Bit Manipulation Primitives Reference

This section provides the syntax, input arguments, value string, and description of each arithmetic primitive. The primitives appear in alphabetical order.

 

bbo - Binary to Octet

 

Syntax

:(bbo,<input>)

 

Input Arguments

<input> One or more binary groups.

 

Description

The value-producing bbo primitive converts binary groups to octets.

 

Example

:(bbo,0011 0101 0010 1100 0100 0101 0111 0101,/)'5, Eu

:(bbo,:(pc,00100001,10))' !!!!!!!!!!

 

bla - AND

 

syntax

:(bla,<arg1>,<arg2>,<z-ret>)

 

Input Arguments

<arg1 > First binary argument.

<arg2> Second binary argument.

  • <z-ret> A value string you supply for the processor to interpret if one of the binary arguments contains a character that is not a 1 or a 0.
  • Description

    The value-producing bla primitive performs a bitwise AND on the <arg1 > and <arg2> values.

     

    Example

    :(bla,1011 0011,0001 1001, error)'00010001

     

    bln - Negation or Complement

     

    syntax

    (bln, <arg>, <z-ret>)

     

    Input Arguments

    <arg> Binary argument.

  • <z-ret> A value string you supply for the processor to interpret if <arg> contains a character that is not a 1 or a 0.
  • Description

    The value-producing bln primitive performs a negation or complement operation on the <arg> value.

     

    Example

    :(bln,01010101,error)' 10101010

     

    blo - OR

     

    syntax

    :(blo,<argl>,<arg2>,<z-ret>)

     

    Input Arguments

    <arg1> First binary argument.

    <arg2> Second binary argument.

  • <z-ret> A value string you supply for the processor to interpret if one of the binary arguments contains a character that is not a 1 or a 0.
  • Description

    The value-producing blo primitive performs a bitwise OR operation on the <argl > and <arg2> values.

     

    Example

    :(blo,0101 0100,1010 0100,error)' 11110100

     

    blx - exclusive OR

     

    syntax

    :(b1x,<arg1>,<arg2>,<z-ret>)

     

    Input Arguments

    <arg1> First binary argument.

    <arg2> Second binary argument.

  • <z-ret> A value string you supply for the processor to interpret if one of the binary arguments contains a character that is not a 1 or a 0.
  • Description

    The value-producing blx primitive performs an exclusive OR operation on the <arg1> and <arg2> values.

     

    Example

    :(blx,00110011,00111100,error)' 00001111

     

    bob - Octet to Binary

     

    Syntax

    :(bob,<input>,<sep>)

     

    Input Arguments

    <input> One or more octet groups.

    <sep> Non-digit characters to separate octet groups.

     

    Description

    The value-producing bob primitive converts octet groups to binary.

     

    Example

    :(bob,lAa,/)' 00110001/01000001/01100001

     

    bon - Octet to Decimal

     

    syntax

    (bon, <input>, <sep>)

     

    Input Arguments

    <input> String.

    <sep> Non-digit characters to separate octet groups.

     

    Description

    The value-producing bon primitive converts groups of up to four octets to an unsigned decimal.

     

    Example

    :(bon,a)' 97

    :(bon,A)' 65

    :(bon,(,))' 44

    :(bon,abcd)' 1633037924

    :(bon,abcdefghijk,//)' 1633837924//1701209960//6908523

     

    box - Octet to Hex

     

    Syntax

    :(box, <input>, <sep>, <z-ret>)

     

    Input Arguments

    <input> One or more octet groups.

    <sep> Non-digit characters to separate octet groups of four octets.

  • <z-ret> A value string you supply for the processor to interpret if the processor does not have enough room in the workspace for conversion.
  • Description

    The value-producing box primitive converts octet groups to hex.

     

    Example

    In the following example, 09 is the hex for a tab character and 2c is hex for a comma.

    :(box,123abcABC( )(,),/)' 31 32 33 61/62 63 41 42/43 09 2c

     

    bxo - Convert Hexadecimal to ASCII

     

    syntax

    :(bxo,<string>,<z-ret>)

     

    Input Arguments

    <string> A string of hexadecimal numbers.

  • <z-ret> A value string you supply for the processor to interpret if the processor does not have enough room in the workspace for conversion.
  • Description

    The value-producing bxo primitive converts a string of hexadecimal numbers into ASCII text characters. You can use this primitive to convert control characters that you cannot enter easily from the keyboard.

     

    Example

    :(bxo,414243)' ABC

    :(bXO, 41 45 65)' AEe

    :(bxo, 0d 0a 65, (:(o„This produces a carriage return, a

  • line feed character, and as e.))
  • )'

    e