【问题标题】:how to debug an NASM assembly program requiring user input?如何调试需要用户输入的 NASM 汇编程序?
【发布时间】:2012-10-23 14:59:04
【问题描述】:

我正在阅读 jeff 的惊人书籍 assembly step by step,我在第 8 章中,他展示了一个汇编程序示例,该程序以这种方式从用户那里获取文件:

SECTION .bss            ; Section containing uninitialized data

    BUFFLEN equ 1024    ; Length of buffer
    Buff:   resb BUFFLEN    ; Text buffer itself

它将文件文本读入Buff,并将所有大写的文本版本输出到不同的文件。

我想在调试模式下运行这个程序来逐步分析所有寄存器的情况。

我正在使用 INSIGHT 在 ubuntu 上运行它。

我是一个完全的初学者。我知道如何使用 Insight 单步执行,但用户需要运行此程序的方式是:

myProgram > outputfile.txt < inputfile.txt

如何在调试器中模拟这一点?

这是完整的来源:

;  Executable name : uppercaser2
;  Version         : 1.0
;  Created date    : 3/25/2009
;  Last update     : 3/25/2009
;  Author          : Jeff Duntemann
;  Description     : A simple program in assembly for Linux, using NASM 2.05,
;    demonstrating simple text file I/O (through redirection) for reading an
;    input file to a buffer in blocks, forcing lowercase characters to 
;    uppercase, and writing the modified buffer to an output file.
;
;  Run it this way:
;    uppercaser2 > (output file) < (input file)  
;
;  Build using these commands:
;    nasm -f elf -g -F stabs uppercaser2.asm
;    ld -o uppercaser2 uppercaser2.o
;
SECTION .bss            ; Section containing uninitialized data

    BUFFLEN equ 1024    ; Length of buffer
    Buff:   resb BUFFLEN    ; Text buffer itself

SECTION .data           ; Section containing initialised data

SECTION .text           ; Section containing code

global  _start          ; Linker needs this to find the entry point!

_start:
    nop         ; This no-op keeps gdb happy...

; Read a buffer full of text from stdin:
read:
    mov eax,3       ; Specify sys_read call
    mov ebx,0       ; Specify File Descriptor 0: Standard Input
    mov ecx,Buff        ; Pass offset of the buffer to read to
    mov edx,BUFFLEN     ; Pass number of bytes to read at one pass
    int 80h         ; Call sys_read to fill the buffer
    mov esi,eax     ; Copy sys_read return value for safekeeping
    cmp eax,0       ; If eax=0, sys_read reached EOF on stdin
    je Done         ; Jump If Equal (to 0, from compare)

; Set up the registers for the process buffer step:
    mov ecx,esi     ; Place the number of bytes read into ecx
    mov ebp,Buff        ; Place address of buffer into ebp
    dec ebp         ; Adjust count to offset

; Go through the buffer and convert lowercase to uppercase characters:
Scan:
    cmp byte [ebp+ecx],61h  ; Test input char against lowercase 'a'
    jb Next         ; If below 'a' in ASCII, not lowercase
    cmp byte [ebp+ecx],7Ah  ; Test input char against lowercase 'z'
    ja Next         ; If above 'z' in ASCII, not lowercase
                ; At this point, we have a lowercase char
    sub byte [ebp+ecx],20h  ; Subtract 20h to give uppercase...
Next:   dec ecx         ; Decrement counter
    jnz Scan        ; If characters remain, loop back

; Write the buffer full of processed text to stdout:
Write:
    mov eax,4       ; Specify sys_write call
    mov ebx,1       ; Specify File Descriptor 1: Standard output
    mov ecx,Buff        ; Pass offset of the buffer
    mov edx,esi     ; Pass the # of bytes of data in the buffer
    int 80h         ; Make kernel call
    jmp read        ; Loop back and load another buffer full

; All done! Let's end this party:
Done:
    mov eax,1       ; Code for Exit Syscall
    mov ebx,0       ; Return a code of zero 
    int 80H         ; Make kernel call

【问题讨论】:

  • 最简单的方法可能是不重定向标准输入/标准输出 - 只需输入一些文本进行转换...
  • 考虑到 Insight 是一个旧的不受支持的 GDB 前端,我建议你学习好的旧控制台 GDB。这是您将获得的最强大的功能。
  • @Linuxios 谢谢你,你能建议如何在 gdb 中做到这一点吗?我没用过
  • @FrankKotler 你能告诉我你的意思吗?
  • @FrankKotler 有什么需要澄清的吗?

标签: linux ubuntu assembly nasm insight


【解决方案1】:

在运行命令中,您可以像这样设置用户输入:

shell$ gdb ./executable  
gdb> break main  
gdb> run user input  

【讨论】:

    【解决方案2】:

    听起来您想将进程附加到 GDB。你可以试试这个。

    shell$ gdb ./uppercaser2
      gdb> list
      gdb> break read
      gdb> run > ouput.txt < input.txt
      gdb> x/5i $eip
    

    在 shell 提示下,您启动 GDB 并将大写字母 2 附加到 GDB。调试符号应该加载,您可以使用列表来检查以显示您的源代码。通过行号或函数名称在所需位置设置断点。使用 GDB 的 run 来使用您的输入和输出文件启动程序。从这里,您可以使用 GDB 命令分析寄存器和单步执行内存。

    【讨论】:

    • 对我来说,gdb 输出:(在 ./hello 中找不到调试符号)其中 hello 是我从 hello.asm 编译的文件有没有办法传递参数(如 C++ 中的 -g 和C)同时编译程序集以便存在调试符号?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    相关资源
    最近更新 更多