Printing “array” from .bss in gdb

主宰稳场 提交于 2019-12-30 23:08:18

问题


my nasm x86 assembly code contains the following:

; 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

Now what i want is to check whether this code works in gdb. However, how do i print array?

print array only returns the $1 = 1.

print array + X unfortunately is an arithmetical operation, i.e. e.g. print array + 50 actually prints 1+50 = 51 and not the non-existent 51st array element.


回答1:


You can do:

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

PS: Your code is broken, you need cmp ecx, [arraylen].




回答2:


; The code should mimic the following C-code:

In addition to wrong boundary noted by Jester, you also have the wrong initialization: your code is equivalent to:

 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?

This is no different from printing array in C, when the source is compiled without debug info:

(gdb) p array
$1 = 0

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

print array + X unfortunately is an arithmetical operation

You can then use:

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



回答3:


ARM example

x86 should be analogous:

.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 session:

(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}

Tested on GDB 8.1, Ubuntu 18.04.



来源:https://stackoverflow.com/questions/32300718/printing-array-from-bss-in-gdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!