1

我正在尝试编写一个程序,它将小写字符串转换为大写,使用缓冲区来存储初始字符串。我遇到的问题是我的程序将打印出一个无限循环的字符,这些字符必须与我给它的字符串相似。

我认为代码中存在的其他问题如下:

  • 一些子程序ret在调用结束时使用。我遇到的问题是弄清楚这些子例程中的哪些实际上不需要 a ret,并且更好地与jmp. 老实说,我在这两者的语义之间有点困惑。例如,调用 with 的子例程ja是否需要ret在调用结束时 'ed ?

  • 我还试图打印出用于转换值的循环的每次迭代中发生的迭代次数。无论出于何种原因,我都会inc反击并决定用PrintNumIter例程打印它,唉,不幸的是,它没有做任何事情。

完整的程序如下。

编码器

bits 32

[section .bss]

        buf: resb 1024                  ;allocate 1024 bytes of memory to buf

[section .data]

        ;*************
        ;* CONSTANTS *
        ;*************

        ;ASCII comparison/conversion

        LowercaseA:     equ 0x61
        LowercaseZ:     equ 0x7A
        SubToUppercase: equ 0x20

        ;IO specifiers/descriptors

        EOF:            equ 0x0

        sys_read:       equ 0x3
        sys_write:      equ 0x4

        stdin:          equ 0x0
        stdout:         equ 0x1
        stderr:         equ 0x2

        ;Kernel Commands/Program Directives

        _exit:          equ 0x1
        exit_success:   equ 0x0
        execute_cmd:    equ 0x80

        ;Memory Usage

        buflen:         equ 0x400   ;1KB of memory


        ;*****************
        ;* NON-CONSTANTS *
        ;*****************

        iteration_count:    db 0
        query :             db "Please enter a string of lowercase characters, and I will output them for you in uppercase ^.^: ", 10   
        querylen :          equ $-query

[section .text]

    global _start
;===========================================
;             Entry Point
;===========================================

_start:
        nop                                         ;keep GDB from complaining
        call    AskUser 
        call    Read
        call    SetupBuf
        call    Scan
        call    Write
        jmp     Exit

;===========================================
;           IO Instructions
;===========================================

Read:
        mov     eax, sys_read                       ;we're going to read in something
        mov     ebx, stdin                          ;where we obtain this is from stdin
        mov     ecx, buf                            ;read data into buf
        mov     edx, buflen                         ;amount of data to read

        int     execute_cmd                         ;invoke kernel to do its bidding
        ret

Write:
        mov     eax, sys_write                      ;we're going to write something
        mov     ebx, stdout                         ;where we output this is going to be in stdout
        mov     ecx, buf                            ;buf goes into ecx; thus, whatever is in ecx gets written out to
        mov     edx, buflen                         ;write the entire buf

        int     execute_cmd                         ;invoke kernel to do its bidding
        ret

AskUser:
        mov     eax, sys_write
        mov     ebx, stdout
        mov     ecx, query
        mov     edx, querylen   

        int     execute_cmd
        ret

PrintNumIter:
        mov     eax, sys_write
        mov     ebx, stdout
        push    ecx                                 ;save ecx's address
        mov     ecx, iteration_count                ;print the value of iteration_count
        mov     edx, 4                              ;print 4 bytes of data

        int     execute_cmd
        pop     ecx                                 ;grab the value back in
        ret
;===========================================
;           Program Preperation
;===========================================

SetupBuf:
        mov     ecx, esi                        ;place the number of bytes read into ecx
        mov     ebp, buf                        ;place the address of buf into ebp
        dec     ebp                             ;decrement buf by 1 to prevent "off by one" error
        ret                                         

;===========================================
;           Conversion Routines     
;===========================================

ToUpper:
        sub     dword [ebp + ecx], SubToLowercase   ;grab the address of buf and sub its value to create uppercase character


Scan:
        call    PrintNumIter                        ;print the current iteration within the loop

        cmp     dword [ebp + ecx], LowercaseA       ;Test input char against lowercase 'a'
        jb      ToUpper                             ;If below 'a' in ASCII, then is not lowercase - goto ToLower

        cmp     dword [ebp + ecx], LowercaseZ       ;Test input char against lowercase 'z'
        ja      ToUpper                             ;If above 'z' in ASCII, then is not lowercase - goto ToLower

        dec     ecx                                 ;decrement ecx by one, so we can get the next character
        inc     byte [iteration_count]              ;increment the __value__ in iteration count by 1
        jnz     Scan                                ;if ecx != 0, then continue the process
        ret

;===========================================

;Next:
;       dec     ecx                             ;decrement ecx by one
;       jnz     Scan                            ;if ecx != 0 scan
;       ret

;===========================================

Exit:
        mov     eax, _exit
        mov     ebx, exit_success

        int     execute_cmd
4

1 回答 1

4

您的问题直接归因于这样一个事实,即您在完成处理后永远不会将 nul 终止符附加到字符串缓冲区的末尾(据我所知,read系统调用不会读回 null)。

不幸的是,由于您的控制流程奇怪,这有点难做,但是更改SetupBuf应该可以解决问题(注意,您可能应该检查您是否没有溢出buf,但是对于 1KB,我怀疑您是否需要担心学习计划):

SetupBuf:
        mov     ecx, esi                        
        mov     ebp, buf
        mov     [ebp+ecx],0  ;make sure the string is nul terminated            
        dec     ebp                             
        ret

请注意

关于另一个似乎困扰你的代码的问题(你已经恰当地注意到了),你奇怪的控制流。如此简单的指南(注意:不是规则,只是指南),希望能帮助您减少意大利面条代码:

  • JMP(和条件跳转)只能用于在同一过程中转到标签,否则您将开始陷入绑定,因为您无法放松。你唯一可以使用跳转的时间是尾调用,但在这个阶段你不应该担心这个,它更混乱。

  • CALL当你要去另一个过程时总是使用,这可以让你用RETN/RET指令正确地返回调用站点,使控制流更加合乎逻辑。

一个简单的例子:

print_num: ;PROC: num to print in ecx, ecx is caller preserved
    push ecx
    push num_format ; "%d\n" 
    call _printf
    sub esp,8 ;cleanup for printf
    retn

print_loop_count: ;PROC: takes no args
    mov ecx,0x10 ;loop 16 times

do_loop: ;LABEL: used as a jump target for the loop
         ;good idea to prefix jump lables with "." to differentiate them
   push ecx ;save ecx
   call print_num ;value to print is already in ecx
   pop ecx ;restore ecx
   dec ecx
   jnz do_loop ;again?

   retn
于 2012-07-03T07:54:12.060 回答