【问题标题】:Printing "array" from .bss in gdb在 gdb 中从 .bss 打印“数组”
【发布时间】:2015-11-24 20:47:50
【问题描述】:

我的 nasm x86 汇编代码包含以下内容:

; The code should mimic the following C-code:
; int a[10];
; for (int i = 0; i < 10; i++){
;    a[i] = i;
; }

SECTION .data
    arraylen dd 10
SECTION .bss
    array RESD 10
SECTION .text
    global main
main:
    mov ecx, 0
    mov eax, 0
loop:
    inc ecx
    mov dword [array+eax*4], ecx
    inc eax
    cmp ecx, arraylen
    jl loop
end:
    mov ebx, 0
    mov eax, 1
    int 0x80

现在我想要检查此代码是否在 gdb 中有效。 但是,我如何打印array

print array 只返回$1 = 1

print array + X 不幸的是是算术运算,即 例如print array + 50 实际上打印 1+50 = 51 而不是不存在的第 51 个数组元素。

【问题讨论】:

    标签: arrays assembly gdb nasm


    【解决方案1】:

    你可以这样做:

    (gdb) x/10 &array
    0x8049618:      1       2       3       4
    0x8049628:      5       6       7       8
    0x8049638:      9       10
    

    PS:你的代码坏了,你需要cmp ecx, [arraylen]

    【讨论】:

    • 谢谢你,但更准确地说,更糟糕的是:我需要写cmp ecx, dword [arraylen]
    【解决方案2】:

    ;该代码应模仿以下 C 代码:

    除了 Jester 指出的错误边界之外,您还有错误的初始化:您的代码相当于:

     for (int i = 0; i < 10; i++) {
       a[i] = i + 1;  // different from stated goal of "a[i] = i;"
     }
    

    但是,我如何打印array

    这与在C 中打印数组没有什么不同,当源编译时没有调试信息:

    (gdb) p array
    $1 = 0
    
    (gdb) p {int[10]}&array
    $2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    

    print array + X 不幸的是一个算术运算

    然后你可以使用:

    (gdb) p $2[4]
    $3 = 4
    

    【讨论】:

    • 天哪,我真的搞砸了,感谢您的信息...将inc ecx 移到mov dword [array+eax*4] 下方,ecx。现在它应该可以工作了......
    【解决方案3】:

    ARM 示例

    x86 应该是类似的:

    .data:
    a1:
        .float 0.0, 0.1, 0.2, 0.3
    a2:
        .word 1, 2, 3, 4
    .text
        /* Register r1 contains the address of a1. */
        ldr r1, =a1
        ldr r2, =a2
    

    GDB 会话:

    (gdb) p (float[4])a1
    $1 = {0, 0.100000001, 0.200000003, 0.300000012}
    (gdb) p (int[4])a2
    $2 = {1, 2, 3, 4}
    (gdb) p (float[4])*$r1
    $5 = {0, 0.100000001, 0.200000003, 0.300000012}
    (gdb) p (int[4])*$r2
    $7 = {1, 2, 3, 4}
    

    在 GDB 8.1、Ubuntu 18.04 上测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多