Y
Y
yuki2021-10-25 17:59:39
assembler
yuki, 2021-10-25 17:59:39

NASM character-by-character addition to a string?

The stack contains a character-by-character decomposed string (a, b, c, d, e - it doesn't matter where the top is, where the bottom is, abstraction). How can you add characters from the stack character by character to the total string? To end up with the string abcde or edcba

NASM compiler

Initial code

%include "io.inc"

section .data
    string db '12345', 0
    len dw $-string
    str2 db ''
    counter dw 2
    
section .text
    global CMAIN
CMAIN:
    mov ebp, esp; for correct debugging
    mov cx, 1
    mov esi, string
    cld
    ;PRINT_STRING string
    ;mov ah, [string+1]
    ;mov byte[str2], ah
    ;PRINT_DEC 2, len
    PRINT_STRING 'Push to stack'
    NEWLINE
    jmp start
start:
    lodsb
    mov byte[str2], al
    PRINT_STRING str2
    push word[str2]
    cmp cx, word[len]
    je middle
    inc cx
    jmp start
middle:
    NEWLINE
    xor cx, cx 
    mov cx, 0
    sub word[len], 1
    jmp add_to_str
add_to_str: 
    pop ax
    ;mov eax, str2
    ;mov [str2 + cx], ax
    mov word[str2], ax
    PRINT_STRING str2
    cmp cx, word[len]
    je end
    inc cx
    jmp add_to_str
end:
    NEWLINE
    PRINT_STRING 'END'
    ret


In the add_to_str label this magic should happen

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
User700, 2021-10-27
@User700

Like this. In a loop until a null character is reached (for example)

str_len dd 0  ; текущая длина
...
str .rb 1024
...
mov edi, str
add edi, [str_len]
rep:
pop eax
test al, al
jz ext
mov [edi], al
inc edi
jmp rep
ext:
sub edi, str
mov [str_len], edi

But why is this necessary? (If it's not the end goal)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question