pack

Pack BCD 68020

Packs two decimal digits from a word into one packed decimal byte.

68020 only: select opt p=68020 before any code or data

XNZVC –––––
  • Changes no flag

Forms

  • pack , ,
  • pack , ,

Operands

Dn
A data register, d0 to d7. More
#imm
A 16-bit adjustment added to the source word before packing, for example #0. More
-(An)
The source address decreases by 2 before reading two bytes. The destination address decreases by 1 before writing one byte, or by 2 if it is sp. More

Each digit occupies the low four bits of a byte in the source word. The ASCII text '42', for example, is $3432; packing it with adjustment #0 gives $42.

pack adds the 16-bit adjustment to the source word, then copies bits 11–8 to the high digit and bits 3–0 to the low digit. With data-register operands, it reads the source’s low word and writes the destination’s low byte, leaving the destination’s upper bits unchanged. With two predecrement operands, it decreases the source address by 2 and reads those two bytes, then decreases the destination address by 1 and writes one byte. If the byte destination is sp, it decreases by 2 to keep the stack pointer even. This processes a string from its end. The flags are unchanged.

unpk reverses the packing, and abcd adds packed decimal values.

opt p=68020
* --- The text "42" as one byte of packed decimal ---
move.w #'42', d0 ; the two ASCII digits: $3432
pack d0, d1, #0 ; d1 = 66, which is $42: a 4 and a 2

* --- A string of digits, packed from its end ---
lea digits+4, a0 ; just past the four digits
lea packed+2, a1 ; just past the two bytes they pack into
pack -(a0), -(a1), #0 ; "34" into $34
pack -(a0), -(a1), #0 ; "12" into $12
move.w packed, d2 ; d2 = 4660, which is $1234
bra END

digits:
    dc.b '1234'
packed:
    ds.b 2

END: