DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Assembly (http://www.daniweb.com/forums/forum125.html)
-   -   comparing return values of subroutines (http://www.daniweb.com/forums/thread120266.html)

sickly_man Apr 21st, 2008 1:55 pm
comparing return values of subroutines
 
hey everybody. i've got what might be a simple question about return values of subroutines in assembly. okay here goes.

say i've got a subroutine that under normal conditions will do a normal return (RET), and under one condition will return zero (RET 0). so now back in main after this function has been called, i need to compare the return value with zero....only i dont know what the syntax is, or rather, how a subroutine's return value can be referenced in main. does anyone know how to do this? can i just do cmp 0??? i havent tried it, but it doesnt seem right. thanks in advance. my code is below. i know it looks liek a lot, its mostly subroutines, and the part i need help with is towards the bottom. THANKS AGAIN

TITLE        Homework 7
;...................................................
STACKSG        SEGMENT PARA STACK 'Stack'
DB 32 dup(0)                        ;32 words, might want to set larger
STACKSG        ENDS
;.....................................................
DATASG SEGMENT PARA 'Data'        ;data segment

prompt1        DB        'Enter a foreground color:$'
prompt2        DB        'Enter a background color:$'

DATASG ENDS
;.................................................
CODESG SEGMENT PARA 'Code'
MAIN        PROC FAR                ;main procedure
        ASSUME SS:STACKSG, DS:DATASG, CS:CODESG
        MOV AX,DATASG                ;Set address of data
        MOV DS,AX                ;Segment in DS


        ; CODE GOES HERE!!!!

get_fore        proc        near

                ; SET CURSOR POSITOIN
                mov ah, 02H
                mov bh, 00
                mov dh, 09
                mov dl, 30
                int 10h

                ; DISPLAY PROMPT TO USER
                mov ah, 09H
                lea bx, prompt1
                int 21H

                ; LOOK FOR KEYBOARD INPUT...STORE IN DL
                mov ah, 0aH
                int 21h                ; will store char in AL
                cmp al, ''        ; compare AL with the empty string
                je empty
                mov dl, al        ; char stored in DL
                ret

        empty:  ret 0                ; return zero if no keyboard input
endp

get_back        proc        near

                ; SET CURSOR POSITOIN
                mov ah, 02H
                mov bh, 00
                mov dh, 13
                mov dl, 30
                int 10h

                ; DISPLAY PROMPT TO USER
                mov ah, 09H
                lea dx, prompt2
                int 21H

                ; LOOK FOR KEYBOARD INPUT...STORE IN DH
                mov ah, 0aH
                int 21h                ; will store char in AL
                cmp al, ''        ; compare AL with the empty string
                je empty2
                mov dh, al        ; char stored in DH
                ret

        empty2:  ret 0                ; return zero if no keyboard input
endp

set_color        proc        near

                ; CHANGE COLOR OF TEXT (FOREGROUND)
                mov ah, 06H
                mov al, 00H
                cmp dl, 72H        ; checking for 'r' -> red
                je red_text
                cmp dl, 67H        ; check for 'g' -> green
                je green_text
                cmp dl, 62H        ; check for 'b' -> blue
                je blue_text
                cmp dl, 79H        ; check for 'y' -> yellow
                je yellow_text
                cmp dl, 77H        ; check for 'w' -> white
                je white_text

                ; SET TEXT COLOR ATTRTIBUTE
red_text:        mov bh, 04H        ; red text attribute
                jmp next
green_text:        mov bh, 02H        ; green text attribute
                jmp next       
blue_text:        mov bh, 01H        ; blue text attribute
                jmp next
yellow_text:        mov bh, 0EH        ; yellow text attribute
                jmp next
white_text:        mov bh, 07H        ; white text attribute
                jmp next

                ; CHANGE COLOR OF BACKGROUND
next:                mov ah, 06H
                mov al, 00H
                cmp dh, 72H        ; checking for 'r' -> red
                je red_bg
                cmp dh, 67H        ; check for 'g' -> green
                je green_bg
                cmp dh, 62H        ; check for 'b' -> blue
                je blue_bg
                cmp dh, 79H        ; check for 'y' -> yellow
                je yellow_bg
                cmp dh, 77H        ; check for 'w' -> white
                je white_bg

                ; SET BG COLOR ATTRIBUTE
red_bg:                mov bh, 04H        ; red bg attribute
                jmp finish
green_bg:        mov bh, 02H        ; green bg attribute
                jmp finish       
blue_bg:        mov bh, 01H        ; blue bg attribute
                jmp finish
yellow_bg:        mov bh, 0EH        ; yellow bg attribute
                jmp finish
white_bg:        mov bh, 07H        ; white bg attribute
                jmp finish

                ; INVOKE INTERRUPT
finish:                mov cx, 0000H
                mov dx, 184FH
                int 10H

                ret
endp

clear_out        proc        near


endp

clear_in        proc        near


endp

start:                CALL get_fore
                cmp ret, 0
                jmp end_it        ; end program
                CALL get_back
                cmp ret, 0
                jmp end_it        ; end program
                CALL set_color
                jmp start
               
;........................................................

end_it: jmp end_it
       



        MOV AX,4C00H                ;End processing
        INT 21H
MAIN        ENDP                        ;End procedure
CODESG        ENDS                        ;End segment
        END MAIN                ;End program

Ancient Dragon Apr 21st, 2008 3:41 pm
Re: comparing return values of subroutines
 
Return values are normally passed back in registers -- its standard practice to put a 16-bit int (short in C) in AX, 32-bit in EAX or DX::AX and 64-bit on EDX::EAX. But you can do it anyway you want. pointers to arrays etc are done the same way.

If those registers contain anything the calling function needs to keep then push them onto the stack before calling the function.

sickly_man Apr 21st, 2008 4:18 pm
Re: comparing return values of subroutines
 
Quote:

Originally Posted by Ancient Dragon (Post 590054)
Return values are normally passed back in registers -- its standard practice to put a 16-bit int (short in C) in AX, 32-bit in EAX or DX::AX and 64-bit on EDX::EAX. But you can do it anyway you want. pointers to arrays etc are done the same way.

If those registers contain anything the calling function needs to keep then push them onto the stack before calling the function.

so i cant actually return a number like in C or C++? in other words, the RETURN statement in my get_fore and get_back subroutines is incorrect/illegal? so i guess i will have to put these values in registers before i do the return statement, right?

it seems to me then, that nothing is actually returned in terms of data that the subroutine computed. is that an accurate statement?

below is my updated code.

TITLE        Homework 7
;...................................................
STACKSG        SEGMENT PARA STACK 'Stack'
DB 32 dup(0)                        ;32 words, might want to set larger
STACKSG        ENDS
;.....................................................
DATASG SEGMENT PARA 'Data'        ;data segment

prompt1        DB        'Enter a foreground color:$'
prompt2        DB        'Enter a background color:$'

DATASG ENDS
;.................................................
CODESG SEGMENT PARA 'Code'
MAIN        PROC FAR                ;main procedure
        ASSUME SS:STACKSG, DS:DATASG, CS:CODESG
        MOV AX,DATASG                ;Set address of data
        MOV DS,AX                ;Segment in DS


        ; CODE GOES HERE!!!!

get_fore        proc        near

                ; SET CURSOR POSITOIN
                mov ah, 02H
                mov bh, 00
                mov dh, 09
                mov dl, 30
                int 10h

                ; DISPLAY PROMPT TO USER
                mov ah, 09H
                lea bx, prompt1
                int 21H

                ; LOOK FOR KEYBOARD INPUT...STORE IN DL
                mov ah, 0aH
                int 21h                ; will store char in AL
                cmp al, ''        ; compare AL with the empty string
                je empty
                mov dl, al        ; char stored in DL
                ret

        empty:  mov dl, 0
                ret                ; return zero if no keyboard input
endp

get_back        proc        near

                ; SET CURSOR POSITOIN
                mov ah, 02H
                mov bh, 00
                mov dh, 13
                mov dl, 30
                int 10h

                ; DISPLAY PROMPT TO USER
                mov ah, 09H
                lea dx, prompt2
                int 21H

                ; LOOK FOR KEYBOARD INPUT...STORE IN DH
                mov ah, 0aH
                int 21h                ; will store char in AL
                cmp al, ''        ; compare AL with the empty string
                je empty2
                mov dh, al        ; char stored in DH
                ret

        empty2: mov dh, 0
                ret                ; return zero if no keyboard input
endp

set_color        proc        near

                ; CHANGE COLOR OF TEXT (FOREGROUND)
                mov ah, 06H
                mov al, 00H
                cmp dl, 72H        ; checking for 'r' -> red
                je red_text
                cmp dl, 67H        ; check for 'g' -> green
                je green_text
                cmp dl, 62H        ; check for 'b' -> blue
                je blue_text
                cmp dl, 79H        ; check for 'y' -> yellow
                je yellow_text
                cmp dl, 77H        ; check for 'w' -> white
                je white_text

                ; SET TEXT COLOR ATTRTIBUTE
red_text:        mov bh, 04H        ; red text attribute
                jmp next
green_text:        mov bh, 02H        ; green text attribute
                jmp next       
blue_text:        mov bh, 01H        ; blue text attribute
                jmp next
yellow_text:        mov bh, 0EH        ; yellow text attribute
                jmp next
white_text:        mov bh, 07H        ; white text attribute
                jmp next

                ; INVOKE INTERRUPT
next:                mov cx, 0000H
                mov dx, 184FH
                int 10H

                ; CHANGE COLOR OF BACKGROUND
                mov ah, 06H
                mov al, 00H
                cmp dh, 72H        ; checking for 'r' -> red
                je red_bg
                cmp dh, 67H        ; check for 'g' -> green
                je green_bg
                cmp dh, 62H        ; check for 'b' -> blue
                je blue_bg
                cmp dh, 79H        ; check for 'y' -> yellow
                je yellow_bg
                cmp dh, 77H        ; check for 'w' -> white
                je white_bg

                ; SET BG COLOR ATTRIBUTE
red_bg:                mov bh, 04H        ; red bg attribute
                jmp finish
green_bg:        mov bh, 02H        ; green bg attribute
                jmp finish       
blue_bg:        mov bh, 01H        ; blue bg attribute
                jmp finish
yellow_bg:        mov bh, 0EH        ; yellow bg attribute
                jmp finish
white_bg:        mov bh, 07H        ; white bg attribute
                jmp finish

                ; INVOKE INTERRUPT
finish:                mov cx, 0000H
                mov dx, 184FH
                int 10H

                ret
endp

clear_out        proc        near


endp

clear_in        proc        near


endp

start:                CALL get_fore
                cmp dl, 0
                je end_it        ; end program
                CALL get_back
                cmp dh, 0
                je end_it        ; end program
                CALL set_color
                jmp start
               
;........................................................

end_it: jmp end_it
       



        MOV AX,4C00H                ;End processing
        INT 21H
MAIN        ENDP                        ;End procedure
CODESG        ENDS                        ;End segment
        END MAIN                ;End program

so just for your own edification, this program is supposed to prompt the user twice, once to change the foreground color with char codes (r, g, b, y, w) and once to change the background with the same codes. its supposed to keep prompting until the user enters an empty string after either of the prompts.

so now am i returning values correcntly? thanks. also if anybody has a burning desire to run my code and check it out feel free! :icon_cheesygrin:

WaltP Apr 21st, 2008 10:08 pm
Re: comparing return values of subroutines
 
Quote:

Originally Posted by sickly_man (Post 590080)
so i cant actually return a number like in C or C++? in other words, the RETURN statement in my get_fore and get_back subroutines is incorrect/illegal? so i guess i will have to put these values in registers before i do the return statement, right?

it seems to me then, that nothing is actually returned in terms of data that the subroutine computed. is that an accurate statement?

No. As AD said, the return value is in AX. So whatever is in AX is returned.


Quote:

Originally Posted by sickly_man (Post 590080)
so now am i returning values correcntly? thanks. also if anybody has a burning desire to run my code and check it out feel free! :icon_cheesygrin:

Nope. See above.

sickly_man Apr 22nd, 2008 11:16 am
Re: comparing return values of subroutines
 
Quote:

Originally Posted by WaltP (Post 590228)
No. As AD said, the return value is in AX. So whatever is in AX is returned.

Ok so let me get this straight because I want to be clear on this. Does Assembly automatically put all return values of subroutines in AX, or do I have to put it in AX manually with a MOV instruction?

Also what is the syntax for returning zero if it is automatic? can i just say RET 0....as in....

ret 0

????

I know I have a lot of questions, but I'm not an assembly pro...not anywhere near it. So please be clear and articulate with your answers cuz I want to understand rather than just know YES or NO you can't do that in assembly. thanks WaltP and Ancient Dragon for your responses so far...


All times are GMT -4. The time now is 6:43 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC