Monday, March 24, 2014

Looking at variables in Assembly program

We are going to write a demo assembly language program to illustrate data types and variables stored in an assembly language program. Given below is our program.

# Demo program to show how to used Data Types and movx instructions

.data

Helloworld: .ascii "Hello World!"
ByteLocation: .byte 10
Int32: .int 2
Int16: .short 3
Float: .float 10.23
IntegerArray: .int 10,20,30,40,50

.bss
.comm largebuffer, 10000

.text
.globl _start
_start:
        nop
        #Exit Syscall to exit the program
        movl $1, %eax
        movl $0, %ebx
        int $0x80



In one of my former posts I had mentioned that I could not get the symbol files when I started my assembly executable in GDB. Ignoring that fact for the time being I set a break point in the file above and given below is the output of GDB when we examine various register values.

First setting the breakpoint

(gdb) break *_start+1
Breakpoint 3 at 0x8048075

 (gdb) info registers
eax            0x0    0
ecx            0x0    0
edx            0x0    0
ebx            0x0    0
esp            0xbffff340    0xbffff340
ebp            0x0    0x0
esi            0x0    0
edi            0x0    0
eip            0x8048074    0x8048074 <_start>
eflags         0x200296    [ PF AF SF IF ID ]
cs             0x73    115
ss             0x7b    123
ds             0x7b    123
es             0x7b    123
fs             0x0    0
gs             0x0    0


Now if we want to look at the variables and where they are stored on the stack we use the info variables command

(gdb) info variables
All defined variables:

Non-debugging symbols:
0x08049084  Helloworld
0x08049090  ByteLocation
0x08049091  Int32
0x08049095  Int16
0x08049097  Float
0x0804909b  IntegerArray
0x080490af  __bss_start
0x080490af  _edata
0x080490b0  largebuffer
0x0804b7c0  _end


Now lets see how the string Hello World is stored on the stack. We need to use the examine command.

(gdb) x/12cb 0x08049084
0x8049084:    72 'H'    101 'e'    108 'l'    108 'l'    111 'o'    32 ' '    87 'W'    111 'o'
0x804908c:    114 'r'    108 'l'    100 'd'    33 '!'


Examining the next register we again issue the examine command and see value of 10 stored in the bytelocation register.

(gdb) x/1db 0x08049090
0x8049090:    10





                       

No comments:

Post a Comment