It is currently Sat Jan 23, 2021 3:12 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: ASM Write to *Buffer
PostPosted: Tue Jan 12, 2016 10:42 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Sep 15, 2003 5:55 am
Posts: 1344
a total asm-newbie question.

i need to copy some values to a buffer.

this (pseudocode) works:
Code:
  Structure Buffer
    Buffer.b[12]
  EndStructure
  Protected Buffer.Buffer
  ...
  !MOV DWORD [p.v_Buffer], EAX
  !MOV DWORD [p.v_Buffer + 4], EBX
  !MOV DWORD [p.v_Buffer + 8], ECX
  ...
  Debug PeekL(@Buffer)
  Debug PeekL(@Buffer + 4)
  Debug PeekL(@Buffer + 8)

this (pseudocode) doesnt:
Code:
  Protected *Buffer = AllocateMemory(12)
  ...
  !MOV DWORD [p.p_Buffer], EAX
  !MOV DWORD [p.p_Buffer + 4], EBX
  !MOV DWORD [p.p_Buffer + 8], ECX
  ...
  Debug PeekL(*Buffer)
  Debug PeekL(*Buffer + 4)
  Debug PeekL(*Buffer + 8)

can someone please explain what im doing wrong? :)

c ya,
nco2k

_________________
If OSVersion() = #PB_OS_Windows_ME : End : EndIf


Top
 Profile  
Reply with quote  
 Post subject: Re: ASM Write to *Buffer
PostPosted: Tue Jan 12, 2016 11:37 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Sep 12, 2015 6:55 pm
Posts: 105
Code:
Procedure t()
 Protected *Buffer = AllocateMemory(12)
 
  !mov eax, [p.p_Buffer]
       
  !mov dword [eax], $ffffffff
  !mov dword [eax + 4], $fefefefe
  !mov dword [eax + 8], $fdfdfdfd
 
  ShowMemoryViewer(*Buffer, 12)
 
EndProcedure
 
t()
 



You are writing in memory starting ad the address of the pointer variable, not at the address stored inside the pointer.


Top
 Profile  
Reply with quote  
 Post subject: Re: ASM Write to *Buffer
PostPosted: Wed Jan 13, 2016 12:30 am 
Offline
Addict
Addict
User avatar

Joined: Mon Sep 15, 2003 5:55 am
Posts: 1344
cool thanks. :D

but i cant use eax, because eax-edx is filled with data that i need. is edi the right one in this case?
Code:
Procedure t()
 Protected *Buffer = AllocateMemory(12)
 
  !mov edi, [p.p_Buffer]
 
  !mov eax, $ffffffff
  !mov ebx, $fefefefe
  !mov ecx, $fdfdfdfd
 
  !mov dword [edi], eax
  !mov dword [edi + 4], ebx
  !mov dword [edi + 8], ecx
 
  ShowMemoryViewer(*Buffer, 12)
 
EndProcedure

t()
c ya,
nco2k

_________________
If OSVersion() = #PB_OS_Windows_ME : End : EndIf


Top
 Profile  
Reply with quote  
 Post subject: Re: ASM Write to *Buffer
PostPosted: Wed Jan 13, 2016 12:44 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Sep 12, 2015 6:55 pm
Posts: 105
According to the manual you can use eax, ecx and edx freely, but you need to preserve the others, so I think you should push edi (you can use the one you like, more or less) and then pop it before exit.

It should look this way:

Code:
   
  !push edi
 
  !mov edi, [p.p_Buffer]
 
  !mov eax, $ffffffff
  !mov ebx, $fefefefe
  !mov ecx, $fdfdfdfd
 
  !mov dword [edi], eax
  !mov dword [edi + 4], ebx
  !mov dword [edi + 8], ecx
 
  !pop edi
 



but this doesn't work because by using push you alter the stack and PB defines the constant p.p_Buffer as an offset from the current stack pointer (esp).
So after your push you are reading a wrong value from a wrong place.


Code:
; Procedure t()
_Procedure0:
  PS0=8
  XOR    eax,eax
  PUSH   eax
; Protected *Buffer = AllocateMemory(12)
  PUSH   dword 12
  CALL  _PB_AllocateMemory@4
  MOV    dword [esp],eax

p.p_Buffer equ esp+0 ; here it is, esp+0

push edi ; esp changed

mov edi, [p.p_Buffer] ; reading from the wrong place
;



So you may do something like this:

Code:
  !mov eax, [p.p_Buffer]
 
  !push edi
 
  !mov edi, eax
 
  !mov eax, $ffffffff
  !mov ebx, $fefefefe
  !mov ecx, $fdfdfdfd
 
  !mov dword [edi], eax
  !mov dword [edi + 4], ebx
  !mov dword [edi + 8], ecx
 
  !pop edi
 


Or even like this (but this is a little horrid and prone to errors)

Code:

  !push edi
 
  !mov edi, [p.p_Buffer + 4] ; correct for the push
 
  !mov eax, $ffffffff
  !mov ebx, $fefefefe
  !mov ecx, $fdfdfdfd
 
  !mov dword [edi], eax
  !mov dword [edi + 4], ebx
  !mov dword [edi + 8], ecx
 
  !pop edi
 


Top
 Profile  
Reply with quote  
 Post subject: Re: ASM Write to *Buffer
PostPosted: Wed Jan 13, 2016 1:37 am 
Offline
Addict
Addict
User avatar

Joined: Mon Sep 15, 2003 5:55 am
Posts: 1344
thanks a lot. very useful informations :)

c ya,
nco2k

_________________
If OSVersion() = #PB_OS_Windows_ME : End : EndIf


Top
 Profile  
Reply with quote  
 Post subject: Re: ASM Write to *Buffer
PostPosted: Wed Jan 13, 2016 1:50 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1672
in this case it seems you dont need the stack and only need 2 registers if you do each value one-by-one rather than all-at-once? you'd probably have to use an approach more like this anyway if you had more data than the number of registers?
Code:
! mov eax, [p.p_Buffer]
! mov ecx, $ffffffff
! mov dword [eax], ecx
! mov ecx, $fefefefe
! mov dword [eax+4], ecx
! mov ecx, $fdfdfdfd
! mov dword [eax+8], ecx

mines not a very good demo for learning anything about the stack though heehee (still learning here!)

_________________
Thankyou to all the coders who generously helped & encouraged me in the nearly 2yrs when i was welcome here,
it was a tremendous privilege. I learned a lot. I wish you and your families all the best and success for the future.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye