A swap exchanges two values. A rotation moves three values round in a circle, and it can be done with two exchanges.
What to build
Read three keys and print them with the last key first, then the first, then the second.
Requirements
- Keep the three keys in three 8-bit registers.
- Use XCHG instructions (and no spare register) to put them in the order third, first, second.
- Print the three registers in order.
Input
Three characters, then Enter.
Output
The three characters rotated, on one line: the input abc gives cab.
Constraints
The characters are letters or digits.
Example
Input:
abc
Output:
cab
Why it works
Starting from (a, b, c) in (BL, BH, CL), XCHG BL, CL gives (c, b, a), and XCHG BH, CL then gives (c, a, b). Each XCHG changes exactly two registers, so the third stays as it is, which is why two exchanges are enough for a rotation of three values.
Examples
abc
cab
123
312
Hints
Hint 1
Keep the keys in BL, BH and CL, in the order they come.
Hint 2
The wanted order is third, first, second. Two XCHG instructions are enough: first swap BL with CL, then BH with CL.
Hint 3
Work it out on paper first: write the three values under the three register names and apply each XCHG.
Solution
Show one correct solution (try it yourself first)
; Rotate three keys
.MODEL SMALL
.STACK 100H
.CODE
MAIN PROC
MOV AH, 1
INT 21H
MOV BL, AL ; a
INT 21H
MOV BH, AL ; b
INT 21H
MOV CL, AL ; c
XCHG BL, CL ; (c, b, a)
XCHG BH, CL ; (c, a, b)
MOV AH, 2
MOV DL, BL
INT 21H
MOV DL, BH
INT 21H
MOV DL, CL
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