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
What is FASM G
Hard Drive Recovery
Wiring your own LAN
Personal menu
Welcome Guest
User:
Pass:
Register!
Resend Validation Email
 
Forum
 

Clean registers and clean exit., When and how to cleanup registers after use and exit cleanly.

Carruthers
Quote Post


New Member
*

Group: Members
Posts: 2
Member No.: 44502
Joined: 3-June 11


Hi all,
I have been learning assembly for a few weeks and made this little puzzle project,
it is hacked together from various places on the internet and it seems to work fine.
<Project cr9_w>

However i would like some help on cleaning the registers,
before and especially after using them.
I don't really know when and how i should do this correctly.

Also, i havent used a clean exit proc ,my code just returns.
Do i need an exit procedure to ensure my app quits gracefully?
and an example of such would be very nice.
Thanks.
CODE

;Thanks to Goppit for skeleton ASM code,also to Pulsar.
.386
.model flat,stdcall
option casemap:none

include  windows.inc
include  kernel32.inc
include  user32.inc
include     ntdll.inc
includelib kernel32.lib
includelib user32.lib
includelib  ntdll.lib
includelib  masm32.lib

DlgProc  proto  :DWORD,:DWORD,:DWORD,:DWORD
GetCL       proto       :DWORD,:DWORD

.data
password           db "cvqruvur",0
MsgBoxCaption      db "Solved MessageBox",0
MsgBoxText         db "You solved me!",0    
Cmdpass            db 'sJ;7BJ>',0

.data?
hInstance HINSTANCE ?  
PassBuffer     db  32 dup(?)
CmdLineBuffer   db     128 dup(?)

.const
IDC_STATIC  equ     1005
IDD_PUZZLE  equ     1001
IDC_NAME equ  1002
IDC_CHECK equ  1003
IDC_EXIT equ  1004

.code
start:
   invoke GetModuleHandle, NULL
   mov    hInstance,eax    
   invoke DialogBoxParam, hInstance, IDD_PUZZLE, NULL, addr DlgProc, NULL
   invoke ExitProcess,eax
   
DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

.if uMsg == WM_INITDIALOG
invoke GetDlgItem,hWnd,IDC_NAME
invoke SetFocus,eax
.elseif uMsg == WM_COMMAND
mov eax,wParam
.if eax==IDC_CHECK
 invoke GetDlgItemText,hWnd,IDC_NAME,addr PassBuffer,32
 call Generate                                ;if check button pressed do teh bizness.
.elseif eax==IDC_EXIT
 invoke SendMessage,hWnd,WM_CLOSE,0,0
.endif
.elseif uMsg == WM_CLOSE
invoke EndDialog,hWnd,0
.endif        
   xor eax,eax
   ret
DlgProc endp

Generate proc                     ;Here we go.
LOCAL cResult:BOOL
invoke GetCL, 1,addr CmdLineBuffer            ;get 1st command line arg as input.
.if eax==1                                ;does it exist?
   xor eax,eax                                ;clean eax.
   invoke lstrlen, addr CmdLineBuffer          ;get strlen for loop and compare.
   xor ecx,ecx                                ;clean ecx.
   mov ecx, eax
   dec ecx        
   loopB:
   sub CmdLineBuffer[ecx],42      ;subtract 42 from each byte,except 1st byte.
   loop loopB
   
   mov esi, offset CmdLineBuffer  
   mov edi, offset Cmdpass
   invoke lstrcmp, esi,edi                ;cmp to 'sJ;7BJ>'=='stealth'.
   .if eax==0
      mov  eax,TRUE                        ;set a BOOLean to check later.
      mov  cResult,eax
   .endif
.endif

invoke lstrlen, addr PassBuffer
test eax, eax                          ;Is there any input?
jle NOINPUT
.if eax!=8                              ;Is input 8 chars long?
  jmp NOINPUT
.endif
xor ecx,ecx                            ;clean ecx.
mov ecx,7                              ;set counter register to 7.                    
loopA:
xor PassBuffer[ecx] ,23          ;xor each pass byte by 23,except 1st byte.
loop loopA

mov esi,offset PassBuffer
mov edi,offset password
xor ecx,ecx                        ;clean ecx.
mov ecx,8                          ;hardcoded pass length.
cld                                     ;compare resulting string to
repe cmpsb                        ;"cvqruvur"  =='cafebabe'.
jz WINPUT

NOINPUT:
ret

WINPUT:
   .if cResult==TRUE           ;check the BOOLean for cmdline arg
         invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK
   .endif
   xor eax,eax
   ret
Generate endp
end start

PS: If you see anything else thats wrong please tell me so i can learn. :coder

Attached File ( Number of downloads: 5 )
 Login or Register to download

Sponsored Links
PM
Top
shoorick
Quote Post


Extremely Active Member
******

Group: Admins
Posts: 2342
Member No.: 160
Joined: 22-June 04


generally, there is no any need in clearing registers.
you have to load certain values into certain registers due to algorytm of the program to get certain result.

result is usually returned via eax or edx/eax pair.

to provide successfull exitcode program must load zero into eax, it can be or mov eax,0 , either xor eax,eax , or even any other tricky method to load zero into eax.

value of eax can be obtain via ERRORLEVEL if program has run in batch sequence.
PMEmail PosterUsers Website
Top
Carruthers
Quote Post


New Member
*

Group: Members
Posts: 2
Member No.: 44502
Joined: 3-June 11


Clear and well explained, thnx shoorick.
PM
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

Topic Options 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