在 gdb 中从 .bss 打印 "array"

Printing "array" from .bss in gdb

我的 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只有returns = 1.

不幸的是,

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

你可以这样做:

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

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

; The code should mimic the following C-code:

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

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

However, how do i print array?

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

(gdb) p array
 = 0

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

print array + X unfortunately is an arithmetical operation

然后您可以使用:

(gdb) p [4]
 = 4

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
 = {0, 0.100000001, 0.200000003, 0.300000012}
(gdb) p (int[4])a2
 = {1, 2, 3, 4}
(gdb) p (float[4])*$r1
 = {0, 0.100000001, 0.200000003, 0.300000012}
(gdb) p (int[4])*$r2
 = {1, 2, 3, 4}

在 GDB 8.1、Ubuntu 18.04 上测试。