News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

[new question] string processing

Started by JayJay, December 14, 2011, 08:00:32 PM

Previous topic - Next topic

bomz

QuoteI thought it was not allowed to put a .data section in a proc.



Sometimes I do this (put data in code section) to make prog 512 bytes smaller. If it 1.5 kb for ex, make it 1.0 kb

dedndave

i think you can forward reference if you use...
main    PROC
        invoke  MessageBox,0,ustr$(ebx),ADDR byte ptr var1,MB_OK
        ret
var1    db "test",0
main    ENDP

        END     main

or
main    PROC
        invoke  MessageBox,0,ustr$(ebx),offset byte ptr var1,MB_OK
        ret
var1    db "test",0
main    ENDP

        END     main

on the first pass, the assembler needs a type - not an address

at least, that's how we used to do it for .COM files using MASM v 5.10 and earlier
all the data was at the end of the code section

dedndave

Bomz...
you can put data in the code section, but you cannot write to it without using VirtualProtect to alter the permissions

bomz



If I may do this a make prog 1 kb smaller

bomz

Quote.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

.data
mestitle   db "self-modifying code",0
form      db "EAX: %u", 0

.data?
buffer      db 512 dup(?)
OldProtect   dd ?

.code
start:
jmp DATASECTOR
var      dd 0
DATASECTOR:
;Invoke GetCurrentProcessId
;invoke  OpenProcess,PROCESS_VM_OPERATION,FALSE,eax
Invoke VirtualProtect, 4198400, 512, PAGE_READWRITE, addr OldProtect
;invoke GetLastError

mov var, 1
mov eax, var

invoke wsprintf,ADDR buffer,ADDR form,eax
invoke MessageBox,0,ADDR buffer,ADDR mestitle,MB_ICONASTERISK
invoke ExitProcess,0
end start

;PAGE_EXECUTE_READWRITE
;ERROR_NOACCESS
;998 (0x3E6)

dedndave

Invoke VirtualProtect, 4198400, 512, PAGE_READWRITE, addr OldProtect
you got what you asked for on that one   :P

Invoke VirtualProtect, offset var, 4, PAGE_EXECUTE_READWRITE, addr OldProtect

notice that - when the permission of 1 byte is changed, the entire "page" of memory changes with it
pages are, as far as i know, always 4 kb

bomz

to make write code - you need one variable - OldProtect

dedndave

create it on the stack
push eax
Invoke VirtualProtect, offset var, 4, PAGE_EXECUTE_READWRITE, esp
pop edx    ;EDX = old protection value


the problems with your original code:
1) you "hard-wired" the address
2) you did not allow EXECUTE in the code section   :P

bomz




I have no any idea for self-modyfing reason

jj2007

Complete example:
include \masm32\include\masm32rt.inc

.code
AppName db "Masm is great:", 0
start: push eax
mov edi, offset AppName
Invoke VirtualProtect, edi, 4, PAGE_EXECUTE_READWRITE, esp
mov [edi], "zmob"
Invoke VirtualProtect, edi, 4, PAGE_EXECUTE, esp
pop edx ; EDX = old protection value
MsgBox 0, str$(eax), edi, MB_OK ; show return value
mov [edi], "msaM" ; let it crash
MsgBox 0, str$(eax), edi, MB_OK ; you won't see this one
exit
end start

bomz

ERROR in the END
Quoteinclude \masm32\include\masm32rt.inc

.code
AppName db "Masm is great:", 0
start: push eax
mov edi, offset AppName
Invoke VirtualProtect, edi, 4, PAGE_EXECUTE_READWRITE, esp
mov dword ptr[edi], "zmob"
Invoke VirtualProtect, edi, 4, PAGE_EXECUTE, esp
pop edx ; EDX = old protection value
MsgBox 0, str$(eax), edi, MB_OK ; show return value
mov dword ptr[edi], "msaM" ; let it crash
MsgBox 0, str$(eax), edi, MB_OK ; you won't see this one
invoke ExitProcess,0
exit
end start

dedndave

mov dword ptr[edi], "msaM" ; let it crash <--------
Jochen put that error in there for demonstration purposes

JayJay

#27
Hi,

Although i said i fixed it. I actually didn't. I thought so.... Because when i define a .data section within the proc. i can't use the name of the var in another proc.

I am trying to make a small language that converts code tot asssembly.

What i am trying to achieve is that i have a string at local level. So it only exists in the procedure.

for example

void test()
{
    string str = "masm32 is fun";
}


would become but this doesn't work. :(

test PROC
   str     db "masm32 is fun",0
   ret
test ENDP




clive

You again seem to miss the point that the processor can't execute ASCII characters, you have to jump over them. With C it allocates space on the stack and copies the data there, it does not embedded it in-line as ASCII characters.

To use the same name repetitively you'll need to do it like this.

main1 PROC
        jmp @F
var1:
      db "test",0
@@:
      invoke  MessageBox,0,ustr$(ebx),ADDR var1,MB_OK
      ret
main1 ENDP

main2 PROC
        jmp @F
var1:
      db "test",0
@@:
      invoke  MessageBox,0,ustr$(ebx),ADDR var1,MB_OK
      ret
main2 ENDP
It could be a random act of randomness. Those happen a lot as well.

donkey

As clive said, you can't execute ascii strings, but the processor will try since all it does is consume bytes regardless of what you intended them for. So for example:

DB "Hello",0

is encoded as

048h, 065h, 06Ch, 06Ch, 06Fh, 00h

When the processor sees that byte sequence it will execute it as follows:

0040435c 48              dec     eax
0040435d 656c            ins     byte ptr es:[edi],dx
0040435f 6c              ins     byte ptr es:[edi],dx
00404360 6f              outs    dx,dword ptr [esi]
00404361 0000            add     byte ptr [eax],al


Obviously not what you wanted. So you have to jump past it or store it in an area that is not executed, normally it would be stored in either the DATA?, DATA or CONST section, however if its never going to be modified you can store it in the CODE section as you tried above. You can put it anywhere as long as it is not addressed before being declared (MASM limitation, GoAsm does not care),  however to make sure that the processor does not try to execute it you must jump over it using a JMP instruction.

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable