My homework assignment is to convert this c++ code into assembly. Here is the code

void maxInt (int xVal, int yVal, int& big)
{
     big = xVal;
     if ( big < yVal )
          big = yVal;
     return;
}

I have successfully created a program that will find the larger integer and return it, but we have to use the STDCALL technique. I am not real sure on how to do that. Would I push xVal, yVal, and big on the stack, then call maxInt? The code I have now just stores the entered integers in xVal and yVal and uses eax to do all my cmp's and everything and I don't push or pop anything on the stack. Here is my code. I use the Irvine routine that came with my book but everything is pretty self explanitory if you aren't familiar with these. Any help or pointers would be greatly appreciated.

INCLUDE Irvine32.inc
.data

xVal		DWORD	?
yVal		DWORD	?

Message BYTE "Enter each integer on sepatate line. Enter -9999 for both to quit.",0dh,0ah,0
MessageE BYTE "Integers are equal.",0
MessageB BYTE "The larger integer was: ",0

.code
main PROC
	mov	 edx,OFFSET Message
	call WriteString
	call	ReadInt				; Enter first integer
	mov	xVal,eax				; Stores the first value in xVal
	call	ReadInt				; Enter second integer
	mov	yVal,eax				; Stores the second value in yVal
	call	maxInt				; Calls the maxInt PROC
	call crlf					; Carriage return line feed
	call main					; Goes back to main

main ENDP

;---------------------------------------------------------------
maxInt PROC
;
; Calculates and returns the larger of the two integers entered by
; the user.
;
; Receives:	xVal, yVal, the two integers
; Returns:	eax
;---------------------------------------------------------------

	mov	eax,xVal				; Moves xVal to eax
	cmp	yVal,eax				; Compares yVal to eax
	je	L1					; Jumps to L1 if equal
	jg	L3					; Jumps to L3 if eax > yVal
	jl	L4					; Jumps to L4 if eax < yVal
	
L1:	cmp	eax,-9999				; Compares -9999 to eax
	jne	L2					; Jumps to L2 if eax does not equal -9999
	exit						; Exits program if eax equals -9999
	
L2:	mov	 edx,OFFSET MessageE
	call	WriteString
	ret						; Returns back to main

L3:	mov eax,yVal				; Moves the larger integer(yVal) to eax
	jmp L4					; Jumps to L4

L4:	mov	 edx,OFFSET MessageB
	call WriteString
	call	WriteInt				; Displays the larger integer
	ret						; Returns back to main

maxInt ENDP					; Ends maxInt PROC

END main

Recommended Answers

All 6 Replies

;Hi.
;Actually why did you compare eax with -9999 ?
cmp eax,-9999 ;?
----------------------------------------------------------------
;stdcall is pushing on the stack values in the opposite order:
push val2
push val1
call myfunc

Entering -9999 for both ends the program. Ok, I think I understand whats goin on now. I'll try that and see what happens.

Not to be nit-picky, but void really don't have a "return".
And since maxInt never really had an input, perhaps there should
be two functions since maxInt just checks the comparisons.

bool maxInt(int, int, int&)
{
    ...return true if x and y have a diffrence...
}

void printInt(int x, int y, int big)
{
    if(x == -9999) { ...set kill signal... }

    bool difference = maxInt(x, y, big);
    if(!diffrence) { cout << "They were the same" << endl; }
    else
    {
       cout << "Big is: " << big << endl;
    }
}

I suppose it doesn't matter if you have it all in one function or not. Personal preference in design.

Is the right? I'm using the push and everything. I'm still not using all the variable though(xVal, yVal, and &big). How would I incorporate those into my program?

INCLUDE Irvine32.inc
.data

xVal	DWORD	?
yVal	DWORD	?
big	DWORD	?

Message BYTE "Enter each integer on sepatate line. Enter -9999 for both to quit.",0dh,0ah,0
MessageE BYTE "Integers are equal.",0
MessageB BYTE "The larger integer was: ",0

.code
main PROC
	mov	edx,OFFSET Message
	call WriteString
	call	ReadInt				; Enter first integer
	mov	xVal,eax				
	call	ReadInt				; Enter second integer
	mov	yVal,eax
	push yVal
	push xVal
	call	maxInt				; Calls the maxInt PROC
	call crlf					; Carriage return line feed
	call main					; Goes back to main

main ENDP

;---------------------------------------------------------------
maxInt PROC
;
; Calculates and returns the larger of the two integers entered by
; the user.
;
; Receives:	xVal, yVal, the two integers
; Returns:	eax
;---------------------------------------------------------------

	push	ebp
	mov	ebp,esp				; Base of stack frame
	mov	eax,[ebp + 12]			; second integer
	mov	yVal,eax
	mov	eax,[ebp + 8]			; first integer
	cmp	yVal,eax
	jg	L1
	jl	L5
	je	L3
		
L1:	mov	eax,yVal
	jmp	L5
	
L3:	cmp	eax,-9999
	jne	L4
	exit
	
L4:	mov	edx,OFFSET messageE
	call	WriteString
	pop	ebp
	ret	8
	
L5:	mov	edx,OFFSET messageB
	call WriteString
	call	WriteInt
	pop	ebp
	ret	8

maxInt ENDP					; Ends maxInt PROC

END main
INCLUDE Irvine32.inc
maxInt proto xVal:DWORD, yVal:DWORD
;.....................
push 2 ; yVal
push 3 ; xVal
call maxInt
;.....................
maxInt proc xVal:DWORD, yVal:DWORD
     mov eax, xVal
     cmp eax, yVal
     jl lbl1
     jg lbl2
     cmp eax, -9999
     jne lbl3
     ;exit
     lbl3:
     ;they are equal
     ;exit
     lbl1:
     ;yVal more
     ;exit
     lbl2:
     ;xVal more
     ;exit
maxInt endp

;didnt try must work.

INCLUDE Irvine32.inc
maxInt proto xVal:DWORD, yVal:DWORD
;.....................
push 2 ; yVal
push 3 ; xVal
call maxInt
;.....................
maxInt proc xVal:DWORD, yVal:DWORD
     mov eax, xVal
     cmp eax, yVal
     jl lbl1
     jg lbl2
     cmp eax, -9999
     jne lbl3
     ;exit
     lbl3:
     ;they are equal
     ;exit
     lbl1:
     ;yVal more
     ;exit
     lbl2:
     ;xVal more
     ;exit
maxInt endp

;didnt try must work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.