AAJ
AAJ

Reputation: 65

What is the purpose/function of these variable assignments in assembly?

var_30= qword ptr -30h
var_24= dword ptr -24h
var_20= dword ptr -20h
var_1C= dword ptr -1Ch
var_18= dword ptr -18h
var_14= dword ptr -14h
var_10= dword ptr -10h
var_8= qword ptr -8

After disassembling a binary, I found this at the very top of the source file. There was nothing else above it, I am fairly certain it has something to do with command line input, but I am not sure. So does it have something to do with command line input. Also, I was wondering exactly what the equal and minus signs have to do with assigning the variable.

Upvotes: 1

Views: 1275

Answers (1)

Margaret Bloom
Margaret Bloom

Reputation: 44076

To make the listing more understandable some disassemblers try to interpret common code patterns.

One of such pattern is to access local variables with a negative offset with respect to the frame pointer but this may also be triggered by code that handles data in the stack without a frame pointer.

This purposefully unoptimized 32-bit program1 computes the n-th triangular number using a frame-pointer and two local vars:

push ebp
mov ebp, esp

sub esp, 08h

push ebx


mov DWORD PTR [ebp-04h], 1
mov DWORD PTR [ebp-08h], 0

_loop:
 mov ebx, DWORD PTR [ebp-04h]
 cmp ebx, DWORD PTR [ebp+08h]
  ja _end

 mov ebx, DWORD PTR [ebp-04h]
 add DWORD PTR [ebp-08h], ebx

 inc DWORD PTR [ebp-04h]
jmp _loop


_end:
mov eax, DWORD PTR [ebp-08h]

pop ebx

add esp, 08h

pop ebp
ret 04h

1 The program structure, the layout of the loop and the usage of local variables is exaggerated for didactic purpose.

The disassembler sees the accesses to [ebp-xx] and gives them a name:

;var_8 = DWORD PTR -8
;var_4 = DWORD PTR -4
;par_8 = DWORD PTR 8
push ebp
mov ebp, esp

sub esp, 08h

push ebx


mov DWORD PTR [ebp+var_4], 1
mov DWORD PTR [ebp+var_8], 0

_loop:
 mov ebx, DWORD PTR [ebp+var_4]
 cmp ebx, DWORD PTR [ebp+par_8]
  ja _end

 mov ebx, DWORD PTR [ebp+var_4]
 add DWORD PTR [ebp+var_8], ebx

 inc DWORD PTR [ebp+var_4]
jmp _loop


_end:
mov eax, DWORD PTR [ebp+var_8]
pop ebx

add esp, 08h

pop ebp
ret 04h

Usually, one can rename these var as they go through the reverse engineering:

;sum = DWORD PTR -8
;i = DWORD PTR -4
;n = DWORD PTR 8
push ebp
mov ebp, esp

sub esp, 08h

push ebx


mov DWORD PTR [ebp+i], 1               ;Start from i=1...
mov DWORD PTR [ebp+sum], 0             ;Result is 0 initially

_loop:
 mov ebx, DWORD PTR [ebp+i]            
 cmp ebx, DWORD PTR [ebp+n]
  ja _end                               ;... to i <= n

 mov ebx, DWORD PTR [ebp+i]
 add DWORD PTR [ebp+sum], ebx           ;sum += i

 inc DWORD PTR [ebp+i]                  ;i++
jmp _loop


_end:
mov eax, DWORD PTR [ebp+sum]
pop ebx

add esp, 08h

pop ebp
ret 04h

Upvotes: 1

Related Questions