
Examples of assembler code and algorithms for solving the line flipping problem
Given - null-terminated string located in memory at some address. Let's say kukaga, 0x0h.
The task is to get the output on the contrary, “agakuk”, 0x0h
More precisely, a pointer to the memory area where the inverted line lies.
Let's see solutions based on i 8080 architecture.
Request to non-x86 architecture assembler experts - give an example of this task in assembler for a familiar architecture. Just purely interesting to compare.
The first option is a loop algorithm. We make a free memory upside down line. The most important thing is not to go beyond memory. We will leave the processing of borders beyond the scope of the example.
DE - string pointer
PUSH D
POP H
DCX H
MVI M, 0
LABEL1:
XCHG
MOV A, M
XCHG
MOV M, A
DCX H
INX D
JNZ LABEL1
INX H
In HL - pointer to an inverted line
Second option:
PUSH D
POP H
DCX H
MVI M, 0
LABEL1:
XCHG
MOV A, M
JZ LABEL2
XCHG
MOV M, A
DCX H
INX D
JMP LABEL1
LABEL2:
In DE - a pointer to an inverted line
A variant with some sort of recursion. Register BC - per line. Here it is also necessary that the stack does not climb into the memory area. And again, the boundaries are beyond the scope of the example.
PUSH B
POP D
DCX D
XCHG
MVI M, 0
XCHG
LABEL1:
LXI H, LABEL1
PUSH H
LDAX B
INX B
DCX D
STAX D
CMP A
RNZ
POP H
INX D
B DE - pointer to a line-changer
Ask, advise, criticize
I did not bring a solution for i8086, since there it comes down to scasb, rep, movsb. Too easy. But if you want, I can write
The task is to get the output on the contrary, “agakuk”, 0x0h
More precisely, a pointer to the memory area where the inverted line lies.
Let's see solutions based on i 8080 architecture.
Request to non-x86 architecture assembler experts - give an example of this task in assembler for a familiar architecture. Just purely interesting to compare.
The first option is a loop algorithm. We make a free memory upside down line. The most important thing is not to go beyond memory. We will leave the processing of borders beyond the scope of the example.
DE - string pointer
PUSH D
POP H
DCX H
MVI M, 0
LABEL1:
XCHG
MOV A, M
XCHG
MOV M, A
DCX H
INX D
JNZ LABEL1
INX H
In HL - pointer to an inverted line
Second option:
PUSH D
POP H
DCX H
MVI M, 0
LABEL1:
XCHG
MOV A, M
JZ LABEL2
XCHG
MOV M, A
DCX H
INX D
JMP LABEL1
LABEL2:
In DE - a pointer to an inverted line
A variant with some sort of recursion. Register BC - per line. Here it is also necessary that the stack does not climb into the memory area. And again, the boundaries are beyond the scope of the example.
PUSH B
POP D
DCX D
XCHG
MVI M, 0
XCHG
LABEL1:
LXI H, LABEL1
PUSH H
LDAX B
INX B
DCX D
STAX D
CMP A
RNZ
POP H
INX D
B DE - pointer to a line-changer
Ask, advise, criticize
I did not bring a solution for i8086, since there it comes down to scasb, rep, movsb. Too easy. But if you want, I can write