WinAsm Studio, The Assembly IDE - Free Downloads, Source Code
Sponsors
Articles
Programming Quick Start
32-bit Assembler is Easy
Porting Iczelion tutorials
What is FASM
Hard Drive Recovery
Wiring your own LAN
Personal menu
Welcome Guest
User:
Pass:
Register!
Resend Validation Email
 
Forum
Pages (2) [1] 2   ( Go to first unread post )

Segment Register concept in Iczelion 3rd tutorial, Need to explain few concept in third tutorial of iczelion........

EnFeR RoI
Quote Post


Member
**

Group: Members
Posts: 20
Member No.: 30972
Joined: 15-January 10


1) Note: Actually under win32, instance handle is the linear address of your program in memory.

Upon returning from a Win32 function, the function's return value, if any, can be found in eax. All other values are returned through variables passed in the function parameter list you defined for the call.
A Win32 function that you call will nearly always preserve the segment registers and the ebx, edi, esi and ebp registers. Conversely, ecx and edx are considered scratch registers and are always undefined upon return from a Win32 function.

2) Note: Don't expect the values of eax, ecx, edx to be preserved across API function calls.

The bottom line is that: when calling an API function, expects return value in eax. If any of your function will be called by Windows, you must also play by the rule: preserve and restore the values of the segment registers, ebx, edi, esi and ebp upon function return else your program will crash very shortly, this includes your window procedure and windows callback functions.
The GetCommandLine call is unnecessary if your program doesn't process a command line. In this example, I show you how to call it in case you need it in your program.
Next is the WinMain call. Here it receives four parameters: the instance handle of our program, the instance handle of the previous instance of our program, the command line and window state at first appearance. Under Win32, there's NO previous instance. Each program is alone in its address space, so the value of hPrevInst is always 0. This is a leftover from the day of Win16 when all instances of a program run in the same address space and an instance wants to know if it's the first instance. Under win16, if hPrevInst is NULL, then this instance is the first one.
Note: You don't have to declare the function name as WinMain. In fact, you have complete freedom in this regard. You don't have to use any WinMain-equivalent function at all. You can paste the codes inside WinMain function next to GetCommandLine and your program will still be able to function perfectly.
Upon returning from WinMain, eax is filled with exit code. We pass that exit code as the parameter to ExitProcess which terminates our application.

QUOTE
  1)How to preserve and restore the values of the segment register???
          2) Explain this two point in own ways??


I am just a student and new in the world of Assembly Language.

Hope you reply soon!!
biggrin.gif

Thanks in advance.
EnFeR RoI.

PMEmail Poster
Top
vulPeCula
Quote Post


Extremely Active Member
******

Group: Members
Posts: 190
Member No.: 16362
Joined: 16-December 08


I hope you are asking that how to restore the values of segment registers(esi,edi,ebp,ebx) in a code in which you have changed it values.

You can store the values of these registers in a separate variable and then when the needs are over you can again put back the values in corresponding registers.


I hope again I have'nt made a wrong post XeS and Sea like I did last time sad.gif .

Sorry again for that post.....

I hope you got it EnFeR RoI.
If still you did'nt get it other members will be helping you out.Be sure about it smile.gif

Regards,
vulPeCula.
PMEmail PosterMSN
Top
EnFeR RoI
Quote Post


Member
**

Group: Members
Posts: 20
Member No.: 30972
Joined: 15-January 10


yes it is what i wanted to know how to preserve and restore the values of segment registers(esi,edi,ebp,ebx) in a code.
Hope other member reply soon!!!

Thanks in advance.
EnFeR RoI.




PMEmail Poster
Top
XeS
Quote Post


Extremely Active Member
******

Group: Members
Posts: 253
Member No.: 15879
Joined: 3-December 08


CODE

MyProc Proc uses esi,edi,ebp,ebx                  <---------
  xor esi,esi
  xor edi,edi
  xor ebx,ebx
 ;ebp is pushed onto the stack if you are using a calling convention which uses
 ;ebp for accessing the local variables
  ret
MyProc Endp


"masm32\help\masm32.chm"
QUOTE

Syntax: label PROC [distance] [langtype] [visibility]
[<prologuearg>]
[USES reglist] [,parameter [:tag]]...
[LOCAL varlist]
statements
label ENDP


Do the job on your own:
CODE


MyProc:
 pushad;(E)AX, (E)CX, (E)DX, (E)BX, (E)SP, (E)BP, (E)SI, (E)DI
    ;or you are going to push an pop only the registers you need
    ;eax,ecx,edx are assumed that the state is undefined after a call to a function
    ;the caller have to pay attention for this

;do something with registers or not in your procedure
 popad ;(E)AX, (E)CX, (E)DX, (E)BX, (E)SP, (E)BP, (E)SI, (E)DI
 ret

MyProc:
 push ebp
 mov ebp,esp
;save used registers
 push esi
 push edi
 push ebx

;do something

;restore registers
 pop ebx
 pop edi
 pop esi
 mov esp,ebp;leave instruction does the same as this and the following line
 pop ebp
 ret


Quick response because i'm at work...

Damn everytime i have to answer to such topics i'm at work...
But you are all able to figure it out by your self. Write a small example program and use ollydbg to understand whats going on. I really recommend this way.
PMEmail PosterUsers WebsiteICQ
Top
EnFeR RoI
Quote Post


Member
**

Group: Members
Posts: 20
Member No.: 30972
Joined: 15-January 10


Thanks for the reply and clear the concept XeS,next time i will take care for posting any question.

Thanks in advance.
EnFeR RoI.
cheers.gif
PMEmail Poster
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

Topic Options Pages (2) [1] 2  Reply to this topicStart new topicStart Poll

 

Sponsors
Computer Science

Internet
C/C++
Hardware & PC maintenance

HiEditor

General Discussions
Suggestions/Bug Reports
WinAsm Studio

General Discussions
Suggestions/Bug Reports
WinAsm Studio FAQ
Multilingual User Interface
Add-Ins
Assembly Programming

Main
Newbies
Projects
Custom Controls
Snippets
Announcements & Rules

Announcements

General

Online Degrees - Distance Learning
The Heap
Russian
Google