问题描述:

(total newbie to NASM struggling to learn)

I've put the 1st command line argument into register eax. It can be a string between 1-20 lowercase characters.

I now want to loop through this string, copying one character at a time into a byte array A in the program's memory and storing the length of the string N in memory too. At this point in the program I've checked that the string is a legal input and is fine length-wise and case-wise.

This is a rough structure (?) that doesn't seem to be working:

section .bss ; uninitialized data

N resd 1 ; length of string

A resb 1 ; byte array A

section .text

asm_main:

// legal input checking code

mov edx, 0

loop2:

mov al, [eax+edx]

mov [A+edx],al

inc edx

cmp al, 0

jz done_loop2

jmp loop2

done_loop2:

mov [N], edx

call print_int

mov eax, A

call print_string

// code for jumps to errors and end of main

(I'm only printing the size and string to check if the loops works correctly)

I'm getting unexpected outputs: for eg

input "hello" gives me

-6193920

hxETmcxbt=Se6o=Eaco/oa

Any help would be super appreciated, thank you! :)

网友答案:

I'll give you an example:

    global asm_main

SECTION .data
        global x
        mystring: db "stackoverflow",0
        mystringl: equ $-mystring
        array: TIMES mystringl dd 0
SECTION .text

asm_main:
        enter 0,0
        pusha
        mov ecx, 0                      ; counter

looper:
        cmp byte[mystring+ecx], 0x0     ; check for end of string
        je exit

        mov eax, 0                      ; empty eax
        mov al, byte[mystring+ecx]      ; move string position into al
        mov byte[array+ecx], al         ; write into memory

        push eax
        mov eax, 0
        mov al, byte[array+ecx]
        call print_char
        call print_nl
        pop eax

        add ecx, 1
        jmp looper

exit:

If you need any help, feel free to message me.

相关阅读:
Top