Menu
CoddyTech

Invertire il testo

Fa parte della sezione Funzioni, stack e stringhe del percorso Assembly di Coddy. Lezione 25 di 33.

Per invertire un testo, trovane prima la lunghezza. L’ultimo byte del testo va quindi alla posizione 0 del buffer, il byte precedente alla posizione 1 e così via: il byte i del risultato è il byte length - 1 - i del testo.

Bastano due registri indice: rcx percorre il testo all’indietro, partendo dalla fine, e r8 percorre il buffer in avanti, partendo da 0.

.copy:
    test rcx, rcx
    jz .done
    dec rcx                     ; il byte precedente
    movzx edx, byte [rdi + rcx]
    mov [rsi + r8], dl
    inc r8                      ; il prossimo byte libero
    jmp .copy
.done:

Anche lo stack inverte l'ordine: inserisci ogni byte, poi estraili e rimettili nel buffer. L'ultimo a entrare è il primo a uscire, quindi il primo byte restituito è l'ultimo inserito.

.push_bytes:
    movzx eax, byte [rdi + rcx]
    test eax, eax
    jz .pop_bytes
    push rax
    inc rcx
    jmp .push_bytes

Ogni push occupa 8 byte, anche per un solo carattere, quindi un testo di 255 byte usa circa 2 KB dello stack. Esegui pop tante volte quante hai eseguito push, altrimenti ret troverà un byte invece del suo indirizzo di ritorno.

challenge icon

Sfida

Facile

solve riceve l'indirizzo di un testo in rdi, terminato da un byte zero, e un buffer in rsi. Scrivi il testo nel buffer al contrario, partendo dall'ultimo byte, e restituiscine la lunghezza in rax.

Esempio: per l'input hello, il programma stampa olleh.

Scrivi il tuo codice in solution.asm. Il file bloccato main.asm legge una riga di testo, chiama solve e stampa i primi rax byte del buffer.

Provalo tu

bits 64
section .data
    changed_rbx db 'solve changed rbx', 10
    changed_rbp db 'solve changed rbp', 10
    changed_r12 db 'solve changed r12', 10
    changed_r13 db 'solve changed r13', 10
    changed_r14 db 'solve changed r14', 10
    changed_r15 db 'solve changed r15', 10
section .bss
    input_buffer resb 257
    text_buffer resb 257
section .text
    global _start
    extern solve
_start:
    xor eax, eax
    xor edi, edi
    mov rsi, input_buffer
    mov edx, 255
    syscall
    test rax, rax
    jg .trim
    xor eax, eax
.trim:
    test rax, rax
    jz .ended
    movzx edx, byte [input_buffer + rax - 1]
    cmp dl, 10
    je .drop
    cmp dl, 13
    jne .ended
.drop:
    dec rax
    jmp .trim
.ended:
    mov byte [input_buffer + rax], 0
    mov rbx, 0x1111111111111111
    mov rbp, 0x2222222222222222
    mov r12, 0x3333333333333333
    mov r13, 0x4444444444444444
    mov r14, 0x5555555555555555
    mov r15, 0x6666666666666666
    lea rdi, [input_buffer]
    lea rsi, [text_buffer]
    xor eax, eax
    call solve
    mov rdx, 0x1111111111111111
    lea rsi, [changed_rbx]
    cmp rbx, rdx
    jne .changed
    mov rdx, 0x2222222222222222
    lea rsi, [changed_rbp]
    cmp rbp, rdx
    jne .changed
    mov rdx, 0x3333333333333333
    lea rsi, [changed_r12]
    cmp r12, rdx
    jne .changed
    mov rdx, 0x4444444444444444
    lea rsi, [changed_r13]
    cmp r13, rdx
    jne .changed
    mov rdx, 0x5555555555555555
    lea rsi, [changed_r14]
    cmp r14, rdx
    jne .changed
    mov rdx, 0x6666666666666666
    lea rsi, [changed_r15]
    cmp r15, rdx
    jne .changed
    cmp rax, 256
    jbe .length_ok
    mov eax, 256
.length_ok:
    mov byte [text_buffer + rax], 10
    lea rdx, [rax + 1]
    lea rsi, [text_buffer]
    mov eax, 1
    mov edi, 1
    syscall
.exit:
    mov eax, 60
    xor edi, edi
    syscall
.changed:
    mov eax, 1
    mov edi, 1
    mov edx, 18
    syscall
    jmp .exit

Tutte le lezioni di Funzioni, stack e stringhe

Esercitati da solo: Playground Assembly