首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为array - Assembly赋值

为array - Assembly赋值
EN

Stack Overflow用户
提问于 2015-05-30 22:07:02
回答 2查看 1.9K关注 0票数 0

我试图从输入文件中读取字符,并将它们放入一个数组中(换行符除外)。

下面是我的代码:

代码语言:javascript
复制
    mov dword [counter], 0
    mov edi, [size]
loop:   
    mov esi, state
    cmp [counter], edi  ; read all chars of the file
    je end_loop
    pushad
    mov eax, 3
    mov ebx, dword [file_desc]
    mov ecx, read_char
    mov edx, 1
    int 0x80
    popad

    cmp byte [read_char], '1'
    je put_char
    cmp byte [read_char], ' '
    je put_char
    jmp loop

put_char:
    mov edx, [read_char]
    mov [esi + counter], edx

    ;; print number of char read from 0 to size-1
    pushad
mov ecx, dword [counter]
push ecx
push printInt
call printf
add esp, 8
popad

;; print char read
    pushad
    push edx
    push printChar
    call printf
    add esp, 8
    popad

    ;; print value stored in state[counter]
    pushad
    push dword [esi + counter]
    push printChar
    call printf
    add esp, 8
    popad

    mov eax, [counter]
    inc eax
    mov [counter], eax

    jmp loop
end_loop:

循环中的打印工作正常,因为我得到了字符编号,我刚刚读取的字符和esi + counter中的字符。

但是,尝试使用以下代码在读取循环后打印它:

代码语言:javascript
复制
    mov dword [counter], 0
    mov edi, [size]
printarray:
    mov esi, state
    cmp [counter], edi
    je end

    pushad
    push dword [esi + counter]
    push printChar
    call printf
    add esp, 8
    popad

    pushad
    mov ecx, [counter]
    inc ecx
    mov [counter], ecx
    popad
    jmp printarray
end:

我得到的只有空格(在我的printChar中,每行都有新的字符行)。

我不明白我读取的值没有存储在数组中。在printarray循环之前,end loopmov dword [counter], 0之间没有代码。

下面是我的数据和bss:

代码语言:javascript
复制
section .data
newLine:    DB "", 10, 0
printInt:   DB "%d", 10, 0
printString:    DB "%s", 10, 0
printChar:  DB "%c", 10, 0
hello:      DB "hello", 10, 0

section .bss
file_name resb 80
file_desc resd 1
WorldLength resd 1
WorldWidth resd 1
generations resd 1
print_freq resd 1
state resb 60*60
read_char resb 1
counter resd 1
size resd 1

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

发布于 2015-05-30 22:31:08

好吧..。

首先,在字节操作时不要使用32位寄存器。我确信,即使您的代码工作正常,一些数据也会被覆盖。

我认为您的问题存在于类似以下语句的某个地方

代码语言:javascript
复制
mov [esi + counter], edx
...
push dword [esi + counter]

它们实际上意味着:“获取计数器的地址并将其添加到esi",我认为这不是您想要的。

在这一点上,-逐个字符读取文件是非常低效的-使用计数器变量而不是ecx是低效的-递增寄存器而不是内存位置本身也是低效的

我已经尽可能地重写了你的代码,我希望它是有价值的。

代码语言:javascript
复制
mov eax, 3
mov ebx, dword [file_desc]
mov ecx, state
mov edx, [size]
int 0x80
; eax now contains the number of bytes read, so why not to use it?

mov ebx, eax
add ebx, state
mov byte [ebx], 0x0 ; this will be end-of-string, although it may not really be necessary

xor ecx, ecx    ; this will be our counter now
_loop:          ; loop is actually an instruction
    cmp ecx, eax
    je _end
    inc ecx     ; ecx++

    mov dl, '\n'
    cmp byte [state + ecx], dl      ; is newline?
    jne _loop                       ; nope? ok, try again

    mov dl, ' '                     ; yes?
    mov byte [state + ecx], dl      ; replace newline with space character
    jmp _loop
_end:

; print the result
mov edx, eax    ; the size - number of bytes read
mov eax, 4
mov ebx, dword [file_desc]
mov ecx, state
int 0x80
票数 0
EN

Stack Overflow用户

发布于 2015-05-30 23:49:34

问题解决了。我应该使用此命令将字符放入数组中:

代码语言:javascript
复制
put_char:
mov dl, [read_char]
mov [esi], dl

mov eax, [counter]
inc eax
mov [counter], eax
inc esi

jmp loop

我删除了打印,这些只用于调试。

谢谢:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30547140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档