从字符串中获取字符并将其用作数组索引... ASM

时间:2015-02-13 19:47:02

标签: arrays assembly printf character nasm

无法使用字符串数组并从中获取每个字符并在相应的ascii索引的频率表中添加1(频率表由ascii值索引):例如,获取字符' a'然后将1加到数组索引的频率表中[' a']。我得到了分段错误,现在出现错误:操作码和操作数的无效组合,谈论mov ax, al 有关问题参数的任何问题请询问。我已经工作了好几个小时了,真的可以用另一双眼来检查我做错了什么(语法/概念如果你看到一个)请帮忙。

更新:我已经把它打印出来了,所以我认为它是"工作&#34 ;;但是我现在正在尝试打印每个数组索引对应的字符。它不会打印我指向的数组的字符(它几乎没有为该字符打印)。

最新更新:我开始工作了。改变了标签.loopa下的一些代码,现在它工作正常! :) 代码如下:

SECTION .data               ; Data section, initialized variables
 array5:     db "Hello, world...", 0
 array5Len:  equ $-array5-1
 asoutput:   db "%s", 0        ; string output
 newline:    db "", 10, 0      ; format for a new line
 acoutput: db "%c: ", 0                 ; output format for character output

SECTION .bss                   ; BSS, uninitialized variables
 arrayq:     resd 128          ; frequency array of the first 127 ascii values initialized to 0 (none have been counted yet)

SECTION .text
  global main           ; the standard gcc entry point
  main:                 ; the program label for the entry point
    push    ebp         ; set up stack frame
    mov     ebp,esp

    mov esi, array5
    mov edi, 0
    mov ebx, arrayq
    mov ecx, array5Len
; get each character of array5 and add 1 to the frequency table of the corresponding ascii value (which the arrayq is indexed by ascii value).
.loopf:
    xor eax, eax
    mov al, [esi]
    ;mov ax, [esi]
    ;mov ax, al
    ;mov cx, ax
    add edi, eax
    mov ebx, 1
    add [arrayq+4*edi], ebx

    mov edi, 0
    add esi, 1
    loop .loopf

    push dword array2
    push dword asoutput
    call printf
    add esp, 8

    push dword newline
    call printf
    add esp, 4

    ;pop ebx    
    mov ebx, arrayq
    mov ecx, 128                            ; size of arrayq
    mov esi, 0                              ;start at beginning
.loopa:
    mov eax, 0
    cmp [ebx+esi], eax
    je .skip
    mov eax, esi
    push ebx
    push ecx
    mov ebx, 4
    cdq
    div ebx
    push eax
    push dword acoutput
    call printf
    add esp, 8
    pop ecx
    pop ebx

    push ebx
    push ecx                                    ; make sure to put ecx (counter) on stack so we don't lose it when calling printf)
    push dword [ebx + esi]                  ; put the value of the array at this (esi) index on the stack to be used by printf
    push dword aoutput                          ; put the array output format on the stack for printf to use
    call printf                                 ; call the printf command
    add esp, 8                                  ; add 4 bytes * 2
    pop ecx                                     ; get ecx back
    pop ebx

    push ebx
    push ecx
    push dword newline
    call printf
    add esp, 4
    pop ecx
    pop ebx

.skip:
    add esi, 4
    loop .loopa
.end:
    mov     esp, ebp    ; takedown stack frame
    pop     ebp         ; same as "leave" op

1 个答案:

答案 0 :(得分:0)

更改.loopa标签下的代码,使其打印索引对应的字符:

    .loopa:
    mov eax, 0
    cmp [ebx+esi], eax
    je .skip
    mov eax, esi
    push ebx
    push ecx
    mov ebx, 4
    cdq
    div ebx
    push eax
    push dword acoutput
    call printf
    add esp, 8
    pop ecx
    pop ebx