📜 ⬆️ ⬇️

Examples of assembler code and algorithms for solving the problem of string flip

Given is a null-terminated string located in memory at some address. Assume "kukaga", 0x0h.
The task is to get the output line on the contrary, “agakuk”, 0x0h
More precisely, the pointer to the memory area where the inverted string is

Let's look at solutions based on i 8080 architecture.

Asking for connoisseurs of assemblers of non-x86 architecture - give an example of this task in an assembler for a familiar architecture. Just purely interesting to compare.
')


The first option is the cycle algorithm. We bring in the free memory line upside down. The most important thing is not to go beyond memory. Border handling is left out of the scope of the example.

DE - pointer to string

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 the inverted string

The 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 string

A variant with a kind of recursion. Register BC - on the line. Here it is also necessary that the stack does not get into the memory area. 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

In DE - a pointer to a string of flipper

Ask, advise, criticize

I did not bring the solution for i8086, since it all boils down to scasb, rep, movsb. Too easy. But if you want - I can write

Source: https://habr.com/ru/post/27474/


All Articles