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

getting character from string and using it as array index... ASM

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

更新:我已经把它打印出来了,所以我认为它是 "working";但是我现在正在尝试打印每个数组索引对应的字符。它不会打印我指向的数组的字符(它实际上不打印任何字符)。

最新更新:我让它工作了。更改了 .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

更改 .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