Log in

View Full Version : Data in Code Section


Ret
July 23rd, 2012, 10:38
greetings..!

question is related to assembly lang...request, if this's a trespass, it may be overlooked for once!

Code:
include c:\masm32\include\masm32rt.inc

.data
var1 dword ?

.code
start:
mov eax, 76543210
mov var1, eax

push offset var1
push eax
call dwtoa

invoke StdOut, offset var1
invoke ExitProcess, 0
end start


comment
the above asm code links to form .obj and .exe file, gives output: 76543210.

1-slightly modifying the above code to:

Code:
.start:

mov eax, 76543210
mov var1, eax

invoke StdOut, offset var1

why this code (without calling dwtoa), doesn't give the same output, although the same value is saved at the address of var1.?

2-next, i would like to declare and save the data, in the code section like:

Code:
invoke StdOut, offset var1
invoke ExitProcess, 0

var1 dword ?
end start


linker doesnot agree to it - how to convince linker to accept it.

thank you..!

blabberer
July 23rd, 2012, 16:40
;data declaration required upfront before start of code (ie before start:
;if not using .data section and want to put data in .text section
;beware this kind of code is vulnerable to exploits
;data should be in .data section which has read write permissions
;but doesnt have execution permission read about dep / aslr /
;use only for satisfying curiosity do not use in real life code
;also note i declare 2 more dwords because dwtoa will use 8 bytes
;if i had declared only one dword the code will be screwed up
;by dwtoa routine actually i reluctantly answer this part of question
;you should use a debugger to watch and learn how to eliminate this
;kind of problems
;also bear in mind after declaring in .code section you need to
;change permissions of code section with /SECTION: linker argument
;see linker command line below

;compile and link with
;ml /c /coff /nologo %1.asm"
;link /nologo /section:.text,rwe /SUBSYSTEM:CONSOLE %1.obj

;also use qualifiers like hex oct bin dec etc which would help you
;in writing good code your 76543210 without h (hex qualifier)
;would be treated as decimal by the compiler (ml.exe) and it will convert it
;to hex viz eax / var1 will hold 048FF4EA which would be converted to
;3736353433323130 for first invoke
; ea f4 8f 04
;will print Ω ⌠ Å ♦ + leftover garbage 3210 for second invoke



Code:


include \masm32\include\masm32rt.inc
.code
var1 dword 3 dup(?)
start:

mov eax, 76543210
mov var1, eax
push offset var1
push eax
call dwtoa

; 7 6 5 4 3 2 1 0
; var1 will contain 3736353433323130 after calling dword to ascii routine
invoke StdOut, offset var1

;the following Stdout will print the dword as ascii
; ie it will print Ω⌠Å♦ + leftover garbage of dwtoa viz 3210 ;
;like Ω⌠Å♦3210

mov eax, 76543210
mov var1, eax
invoke StdOut, offset var1

invoke ExitProcess, 0
end start

Ret
July 24th, 2012, 05:37
Thank you... blabberer..!

your clarification is valued, its right to the point and very commendable.!

Quote:
your 76543210 without h (hex qualifier) would be treated as decimal by the compiler (ml.exe) and it will convert it
to hex viz eax / var1 will hold 048ff4ea which would be converted to 3736353433323130..

adding to the above, your reminder that ascii is 8-bit value, did the rest

Quote:
var1 dword 3 dup (?)


yes - for the time being, it resolves my uncertainty linked to dwtoa ()

The intention for saving data in code section is meant for finding the direct address of the dll's -
- and then locating the addresses of api’s residing within .dlls - meant for using them run time.

say for instance: for locating the address of the kernel32.dll
and then proceed to get the address of ExitProcess that reside within it.

meanwhile, i need to go over your earlier instructions once again, and be trying to follow it to the
best of my understanding. anyway, i be back when iam stuck on the way...which is highly probable.!

thank you once again..!

Ret
July 24th, 2012, 12:04
Quote:
with data defined in data section:
> ml /c /coff 1.asm > link /subsystem: console 1.obj


Code:
include c:\masm32\include\masm32rt.inc
.data

var1 dword 3 dup (?); <---

.code
start:
mov eax, 7543210h
mov var1, eax
push offset var1
push eax
call dwtoa

invoke StdOut, offset var1
invoke ExitProcess, 0
end start


out put: 122958352 = 7543210h
--
Quote:
with data defined in code section:
> ml /c /coff /nologo %3.asm


Code:
include c:\masm32\include\masm32rt.inc
.data

.code
start:
mov eax, 7543210h
mov var1, eax
push offset var1
push eax
call dwtoa

invoke StdOut, offset var1
invoke ExitProcess, 0

var1 dword 3 dup (?) ; <---

end start


if > ml /c /coff /nologo %3.asm is used -
: fatal error a1000: cannot open file: %3.asm

if > ml /c /coff /nologo 3.asm used - without (%)
: error a2006: undefined symbol: var1
: error a2114: invoke argument type mismatch: argument: 1

the assembler fails to see the file when it’s named %3.asm.
the assembler fails to see the var1 when it’s named 3.asm.

how do i get over this hurdle..!?

blabberer
July 24th, 2012, 13:51
Quote:

the assembler fails to see the file when it�s named %3.asm.

you need to pick up basics a bit more

the %1 is a wildcard for batch file scripts

suppose you have a command ml foo.asm in a batch file

if you execute it it will look for foo.asm only but wont compile blah.asm

so instead of ml foo.asm if i have a batchfile named compile.bat

that has

ml %1.asm

i can use it to compile any asm file

all i need to do is



compile.bat foo the %1 will now hold foo so it will compile foo.asm

compile,bat blah the %1 will now hold blah so it will compile blah.asm

compile.bat xyz the %1 will now hold xyz so it will compile xyz.asm

so you need the file named normally


Quote:

the assembler fails to see the var1 when it�s named 3.asm.


this has got nothing to do with file name
you either havent read my earlier reply
or if you have read it
you haven't spent a few minutes trying to understand what was written

or you dont care and cant even copy paste properly

and you dont want to spend time finding out what the error messages mean from the compiler

the compiler explicitly stated you havent defined the symbol didn't you see ????

Quote:

: error a2006: undefined symbol: var1


i wrote you need to declare your variables at the start not at the end also i posted a code snippet

you are using your var1 before it is declared so obviously compiler wont know what the heck it is

var 1 needs forward referancing like

Code:

.code
var1 dword 3 dup(?)
start:

mov eax, 76543210



should not be declared after

ExitProcess()

btw keep in mind ExitProcess is defined as a function that wont return ( __declspec __noreturn ) in headers

so anything you do after ExitProcess Will normally be of no use
they wont be executed if they are not reachable

Ret
July 25th, 2012, 00:48
blabberer..!

i did not remember and shouldnot have forgotton - that a variable should have at least been declared
before its being used - yes i forgot the basics when i needed it - apologies for this inadequacy -

Code:
.code
var1 dword 3 dup (?)
start:
mov eax, 7543210h
mov var1, eax
.. ..
.. ..
invoke stdout, offset var1

the above code as suggested by blabberer with suitable options for linking
and assembling, resolved my difficulty as i stated in - data in code section -

thank you..!