Bytes of a word

The 8086 stores a word with its low byte first, at the lower address. Writing the two halves of a word separately shows where each half goes.

What to build

Build a word from two typed keys, and print its two bytes in the order they lie in memory.

Requirements

  • Define a word W in the data segment.
  • Store the first key in the high byte of W (offset 1) and the second key in the low byte (offset 0).
  • Print the byte at offset 0, then the byte at offset 1.

Input

Two characters, then Enter.

Output

The two characters in memory order: the second key first, then the first key.

Constraints

The characters are letters or digits.

Example

Input:

ab

Output:

ba

Why it works

The high byte of a word is the one with the higher address, so the first key at offset 1 comes second in memory. Reading the bytes from offset 0 on therefore gives the second key first. Reading the whole word with MOV AX, W would put the first key in AH and the second in AL.

Examples

Example 1 · Input
ab
Expected output
ba
Example 2 · Input
xy
Expected output
yx

Hints

Hint 1

Define a word variable: W DW ?.

Hint 2

BYTE PTR W is the byte at offset 0 of W, and BYTE PTR W + 1 is the byte at offset 1.

Hint 3

Store the first key at offset 1 (the high byte) and the second key at offset 0 (the low byte), then print the bytes at offset 0 and offset 1, in that order.

Solution

Show one correct solution (try it yourself first)
; Bytes of a word
.MODEL SMALL
.STACK 100H
.DATA
    W DW ?
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
    MOV AH, 1
    INT 21H
    MOV BYTE PTR W + 1, AL   ; the high byte
    INT 21H
    MOV BYTE PTR W, AL       ; the low byte
    MOV AH, 2
    MOV DL, BYTE PTR W       ; offset 0
    INT 21H
    MOV DL, BYTE PTR W + 1   ; offset 1
    INT 21H
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Variables and the data segment →