【问题标题】:How do I loop through a string and store it into an array in NASM?如何遍历字符串并将其存储到 NASM 中的数组中?
【发布时间】:2016-03-09 23:14:44
【问题描述】:

(NASM 的所有新手都在努力学习)

我已将第一个命令行参数放入寄存器 eax。可以是 1-20 个小写字符的字符串。

我现在想遍历这个字符串,一次将一个字符复制到程序内存中的字节数组 A 中,并将字符串 N 的长度也存储在内存中。在程序的这一点上,我检查了字符串是合法的输入,并且在长度和大小写方面都很好。

这是一个粗略的结构 (?),似乎不起作用:

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

(我只打印大小和字符串来检查循环是否正常工作) 我得到了意想不到的输出:例如 输入“你好”给我 -6193920 hxETmcxbt=Se6o=Eaco/oa

任何帮助将不胜感激,谢谢! :)

【问题讨论】:

    标签: arrays nasm


    【解决方案1】:

    我给你举个例子:

        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:
    

    如果您需要任何帮助,请随时给我发消息。

    【讨论】:

    • 这帮助很大!我让它工作。谢谢你。所以在我保存新的东西之前我需要清空 eax 吗?为什么要清空它然后将其压入堆栈,然后将其弹出?
    • 调用 print_char 或 print 方法使用 EAX 寄存器传递参数
    • 项目进展如何?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2021-10-01
    • 2015-10-02
    • 2014-12-10
    • 2022-01-16
    • 2011-12-24
    • 1970-01-01
    相关资源
    最近更新 更多