Good, I'm doing a work in assembley.
where I have to reverse the string using recursion:
Only by using the following commands:
- LOCO, Lodder, ADDD, SUBD, LODL, ADDL, subl, PUSH, POP, PSHI, POPI, CALL, Retn ...

trabid:. string "hello world"


main:
.
...
...

rec: # recursive function that I will reverse the string!

ps: I already have a print function that does the string, etc ...
Objective: To appear in the output "dlrow olleh"


someone help me? Thank-_ _

Dani AI

Generated

A few focused notes that add to what , and have said and that will be useful later when concrete assembler syntax is chosen.

Two easy strategies (pick one depending on what your print routine accepts)

  • Recursively print characters in reverse: walk to the string end with recursive calls, then print each character on the way back. This needs a single-byte print routine (or a way to form a 1-byte string).
  • Recursively reverse in place: swap first and last bytes, then recurse on the interior substring (start+1, length-2). This avoids needing a single-byte print but requires read/write memory and a way to compute length.

Pseudocode (print-on-return approach)

reverse_print(ptr):
    if *ptr == 0:        ; base case: null terminator
        return
    c = *ptr
    reverse_print(ptr + 1)
    print_char(c)

Generic assembly skeleton for the print-on-return approach (abstract mnemonics)

; argument: pointer to string (in register PTR or on stack)
rec:
    LOAD r0, [PTR]       ; read byte at PTR
    CMP  r0, 0
    JZ   rec_done
    PUSH r0              ; save character across the recursive call
    ADD  PTR, 1
    CALL rec
    POP  r0              ; restore character
    CALL print_char      ; pass r0 appropriately (calling convention)
rec_done:
    RETN

Practical points and common pitfalls

  • Provide the assembler/CPU, calling convention, and whether the string is null-terminated or length-prefixed; syntax and register usage depend on that (as requested).
  • Preserve any caller-saved registers and keep pushes/pops balanced; mismatched stack operations are the usual cause of crashes.
  • Watch off-by-one errors (compute end = ptr + len - 1 for swaps) and recursion depth for very long strings — iterative approaches are safer for extremely long inputs.

Once the exact assembler and the signature of your print routine are known, a concrete, instruction-for-instruction solution can be posted.

Recommended Answers

All 4 Replies

What have you tried so far? What have you thought about trying?

From the forum rules:
- Do provide evidence of having done some work yourself if posting questions from assignments.

What have you tried so far? What have you thought about trying?

From the forum rules:
- Do provide evidence of having done some work yourself if posting questions from assignments.

already made ​​many functions such as counting the pixels of a black and white string, the string to print a screen, the problem is that I have to use recursion and do not know: /

In contrast to regular re-iteration in a repetitive procedure
a recursive function calls itself over and over again to perform its
operation. It takes parameters just as any other function and has a condition
upon which its recursion will stop.

Before we proceed, could you also tell us the assembly program you are using (e.g., MASM, GAS, NASM, FASM, etc.)? Each assembler uses a different variation on the assembly language syntax, and some of them are quite different from the others.

For that matter, while some of the mnemonics you mention look like the GAS versions, but others don't seem familiar at all, which makes me wonder what the CPU you are targeting is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.