Difference between revisions of "CSC231 No-no! and Be-Careful! situations"

From dftwiki3
Jump to: navigation, search
(What's tricky about the following code?)
(What's tricky about the following code?)
Line 31: Line 31:
 
compute:
 
compute:
 
         pushad
 
         pushad
push ebx
+
push ebp
         mov ebx, esp
+
         mov ebp, esp
  
 
;;; get parameters from the stack                                                                                       
 
;;; get parameters from the stack                                                                                       
Line 45: Line 45:
  
 
;;; we're done                                                                                                           
 
;;; we're done                                                                                                           
         pop ebx
+
         pop ebp
 
         popad
 
         popad
 
         ret 3*4
 
         ret 3*4

Revision as of 08:18, 10 November 2010

--D. Thiebaut 13:15, 10 November 2010 (UTC)


What's wrong with the following code?


;------------------------------------------------------------------
; function that pushes several results in the stack
;------------------------------------------------------------------
compute:	
        pushad
        mov	ecx, N		;loop some number of times                                                              
        mov	eax, data1	;get some data                                                                          
        mov	ebx, data2
.for:   call	doSomething	;operate on eax and ebx                                                                 
                                ; on return, eax contains                                                               
                                ; result we're interested in                                                            
        push	eax		;save result in stack                                                                   
        loop	.for

;;; we're done                                                                                                          
        popad
        ret

What's tricky about the following code?

compute:	
        pushad
	push	ebp
        mov	ebp, esp

;;; get parameters from the stack                                                                                       

        mov     ecx,[ebp+XX]    ;XX is some offset                                                                      
        mov	eax,[ebp+YY]	;YY is some offset                                                                      
        mov	ebx,[ebp+ZZ]    ;ZZ is some offset                                                                      
.for:   call	doSomething	;operate on eax and ebx                                                                 
                                ; on return, eax contains                                                               
                                ; result we're interested in                                                            
        loop    .for

;;; we're done                                                                                                          
        pop	ebp
        popad
        ret	3*4