comparing return values of subroutines

Reply

Join Date: Jun 2007
Posts: 42
Reputation: sickly_man is an unknown quantity at this point 
Solved Threads: 0
sickly_man's Avatar
sickly_man sickly_man is offline Offline
Light Poster

comparing return values of subroutines

 
0
  #1
Apr 21st, 2008
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

  1. TITLE Homework 7
  2. ;...................................................
  3. STACKSG SEGMENT PARA STACK 'Stack'
  4. DB 32 dup(0) ;32 words, might want to set larger
  5. STACKSG ENDS
  6. ;.....................................................
  7. DATASG SEGMENT PARA 'Data' ;data segment
  8.  
  9. prompt1 DB 'Enter a foreground color:$'
  10. prompt2 DB 'Enter a background color:$'
  11.  
  12. DATASG ENDS
  13. ;.................................................
  14. CODESG SEGMENT PARA 'Code'
  15. MAIN PROC FAR ;main procedure
  16. ASSUME SS:STACKSG, DS:DATASG, CS:CODESG
  17. MOV AX,DATASG ;Set address of data
  18. MOV DS,AX ;Segment in DS
  19.  
  20.  
  21. ; CODE GOES HERE!!!!
  22.  
  23. get_fore proc near
  24.  
  25. ; SET CURSOR POSITOIN
  26. mov ah, 02H
  27. mov bh, 00
  28. mov dh, 09
  29. mov dl, 30
  30. int 10h
  31.  
  32. ; DISPLAY PROMPT TO USER
  33. mov ah, 09H
  34. lea bx, prompt1
  35. int 21H
  36.  
  37. ; LOOK FOR KEYBOARD INPUT...STORE IN DL
  38. mov ah, 0aH
  39. int 21h ; will store char in AL
  40. cmp al, '' ; compare AL with the empty string
  41. je empty
  42. mov dl, al ; char stored in DL
  43. ret
  44.  
  45. empty: ret 0 ; return zero if no keyboard input
  46. endp
  47.  
  48. get_back proc near
  49.  
  50. ; SET CURSOR POSITOIN
  51. mov ah, 02H
  52. mov bh, 00
  53. mov dh, 13
  54. mov dl, 30
  55. int 10h
  56.  
  57. ; DISPLAY PROMPT TO USER
  58. mov ah, 09H
  59. lea dx, prompt2
  60. int 21H
  61.  
  62. ; LOOK FOR KEYBOARD INPUT...STORE IN DH
  63. mov ah, 0aH
  64. int 21h ; will store char in AL
  65. cmp al, '' ; compare AL with the empty string
  66. je empty2
  67. mov dh, al ; char stored in DH
  68. ret
  69.  
  70. empty2: ret 0 ; return zero if no keyboard input
  71. endp
  72.  
  73. set_color proc near
  74.  
  75. ; CHANGE COLOR OF TEXT (FOREGROUND)
  76. mov ah, 06H
  77. mov al, 00H
  78. cmp dl, 72H ; checking for 'r' -> red
  79. je red_text
  80. cmp dl, 67H ; check for 'g' -> green
  81. je green_text
  82. cmp dl, 62H ; check for 'b' -> blue
  83. je blue_text
  84. cmp dl, 79H ; check for 'y' -> yellow
  85. je yellow_text
  86. cmp dl, 77H ; check for 'w' -> white
  87. je white_text
  88.  
  89. ; SET TEXT COLOR ATTRTIBUTE
  90. red_text: mov bh, 04H ; red text attribute
  91. jmp next
  92. green_text: mov bh, 02H ; green text attribute
  93. jmp next
  94. blue_text: mov bh, 01H ; blue text attribute
  95. jmp next
  96. yellow_text: mov bh, 0EH ; yellow text attribute
  97. jmp next
  98. white_text: mov bh, 07H ; white text attribute
  99. jmp next
  100.  
  101. ; CHANGE COLOR OF BACKGROUND
  102. next: mov ah, 06H
  103. mov al, 00H
  104. cmp dh, 72H ; checking for 'r' -> red
  105. je red_bg
  106. cmp dh, 67H ; check for 'g' -> green
  107. je green_bg
  108. cmp dh, 62H ; check for 'b' -> blue
  109. je blue_bg
  110. cmp dh, 79H ; check for 'y' -> yellow
  111. je yellow_bg
  112. cmp dh, 77H ; check for 'w' -> white
  113. je white_bg
  114.  
  115. ; SET BG COLOR ATTRIBUTE
  116. red_bg: mov bh, 04H ; red bg attribute
  117. jmp finish
  118. green_bg: mov bh, 02H ; green bg attribute
  119. jmp finish
  120. blue_bg: mov bh, 01H ; blue bg attribute
  121. jmp finish
  122. yellow_bg: mov bh, 0EH ; yellow bg attribute
  123. jmp finish
  124. white_bg: mov bh, 07H ; white bg attribute
  125. jmp finish
  126.  
  127. ; INVOKE INTERRUPT
  128. finish: mov cx, 0000H
  129. mov dx, 184FH
  130. int 10H
  131.  
  132. ret
  133. endp
  134.  
  135. clear_out proc near
  136.  
  137.  
  138. endp
  139.  
  140. clear_in proc near
  141.  
  142.  
  143. endp
  144.  
  145. start: CALL get_fore
  146. cmp ret, 0
  147. jmp end_it ; end program
  148. CALL get_back
  149. cmp ret, 0
  150. jmp end_it ; end program
  151. CALL set_color
  152. jmp start
  153.  
  154. ;........................................................
  155.  
  156. end_it: jmp end_it
  157.  
  158.  
  159.  
  160.  
  161. MOV AX,4C00H ;End processing
  162. INT 21H
  163. MAIN ENDP ;End procedure
  164. CODESG ENDS ;End segment
  165. END MAIN ;End program
Henry Phillips
Web Programmer
Adirondack Area Network, LLC
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,142
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1434
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: comparing return values of subroutines

 
0
  #2
Apr 21st, 2008
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.
Last edited by Ancient Dragon; Apr 21st, 2008 at 3:42 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 42
Reputation: sickly_man is an unknown quantity at this point 
Solved Threads: 0
sickly_man's Avatar
sickly_man sickly_man is offline Offline
Light Poster

Re: comparing return values of subroutines

 
0
  #3
Apr 21st, 2008
Originally Posted by Ancient Dragon View Post
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.

  1. TITLE Homework 7
  2. ;...................................................
  3. STACKSG SEGMENT PARA STACK 'Stack'
  4. DB 32 dup(0) ;32 words, might want to set larger
  5. STACKSG ENDS
  6. ;.....................................................
  7. DATASG SEGMENT PARA 'Data' ;data segment
  8.  
  9. prompt1 DB 'Enter a foreground color:$'
  10. prompt2 DB 'Enter a background color:$'
  11.  
  12. DATASG ENDS
  13. ;.................................................
  14. CODESG SEGMENT PARA 'Code'
  15. MAIN PROC FAR ;main procedure
  16. ASSUME SS:STACKSG, DS:DATASG, CS:CODESG
  17. MOV AX,DATASG ;Set address of data
  18. MOV DS,AX ;Segment in DS
  19.  
  20.  
  21. ; CODE GOES HERE!!!!
  22.  
  23. get_fore proc near
  24.  
  25. ; SET CURSOR POSITOIN
  26. mov ah, 02H
  27. mov bh, 00
  28. mov dh, 09
  29. mov dl, 30
  30. int 10h
  31.  
  32. ; DISPLAY PROMPT TO USER
  33. mov ah, 09H
  34. lea bx, prompt1
  35. int 21H
  36.  
  37. ; LOOK FOR KEYBOARD INPUT...STORE IN DL
  38. mov ah, 0aH
  39. int 21h ; will store char in AL
  40. cmp al, '' ; compare AL with the empty string
  41. je empty
  42. mov dl, al ; char stored in DL
  43. ret
  44.  
  45. empty: mov dl, 0
  46. ret ; return zero if no keyboard input
  47. endp
  48.  
  49. get_back proc near
  50.  
  51. ; SET CURSOR POSITOIN
  52. mov ah, 02H
  53. mov bh, 00
  54. mov dh, 13
  55. mov dl, 30
  56. int 10h
  57.  
  58. ; DISPLAY PROMPT TO USER
  59. mov ah, 09H
  60. lea dx, prompt2
  61. int 21H
  62.  
  63. ; LOOK FOR KEYBOARD INPUT...STORE IN DH
  64. mov ah, 0aH
  65. int 21h ; will store char in AL
  66. cmp al, '' ; compare AL with the empty string
  67. je empty2
  68. mov dh, al ; char stored in DH
  69. ret
  70.  
  71. empty2: mov dh, 0
  72. ret ; return zero if no keyboard input
  73. endp
  74.  
  75. set_color proc near
  76.  
  77. ; CHANGE COLOR OF TEXT (FOREGROUND)
  78. mov ah, 06H
  79. mov al, 00H
  80. cmp dl, 72H ; checking for 'r' -> red
  81. je red_text
  82. cmp dl, 67H ; check for 'g' -> green
  83. je green_text
  84. cmp dl, 62H ; check for 'b' -> blue
  85. je blue_text
  86. cmp dl, 79H ; check for 'y' -> yellow
  87. je yellow_text
  88. cmp dl, 77H ; check for 'w' -> white
  89. je white_text
  90.  
  91. ; SET TEXT COLOR ATTRTIBUTE
  92. red_text: mov bh, 04H ; red text attribute
  93. jmp next
  94. green_text: mov bh, 02H ; green text attribute
  95. jmp next
  96. blue_text: mov bh, 01H ; blue text attribute
  97. jmp next
  98. yellow_text: mov bh, 0EH ; yellow text attribute
  99. jmp next
  100. white_text: mov bh, 07H ; white text attribute
  101. jmp next
  102.  
  103. ; INVOKE INTERRUPT
  104. next: mov cx, 0000H
  105. mov dx, 184FH
  106. int 10H
  107.  
  108. ; CHANGE COLOR OF BACKGROUND
  109. mov ah, 06H
  110. mov al, 00H
  111. cmp dh, 72H ; checking for 'r' -> red
  112. je red_bg
  113. cmp dh, 67H ; check for 'g' -> green
  114. je green_bg
  115. cmp dh, 62H ; check for 'b' -> blue
  116. je blue_bg
  117. cmp dh, 79H ; check for 'y' -> yellow
  118. je yellow_bg
  119. cmp dh, 77H ; check for 'w' -> white
  120. je white_bg
  121.  
  122. ; SET BG COLOR ATTRIBUTE
  123. red_bg: mov bh, 04H ; red bg attribute
  124. jmp finish
  125. green_bg: mov bh, 02H ; green bg attribute
  126. jmp finish
  127. blue_bg: mov bh, 01H ; blue bg attribute
  128. jmp finish
  129. yellow_bg: mov bh, 0EH ; yellow bg attribute
  130. jmp finish
  131. white_bg: mov bh, 07H ; white bg attribute
  132. jmp finish
  133.  
  134. ; INVOKE INTERRUPT
  135. finish: mov cx, 0000H
  136. mov dx, 184FH
  137. int 10H
  138.  
  139. ret
  140. endp
  141.  
  142. clear_out proc near
  143.  
  144.  
  145. endp
  146.  
  147. clear_in proc near
  148.  
  149.  
  150. endp
  151.  
  152. start: CALL get_fore
  153. cmp dl, 0
  154. je end_it ; end program
  155. CALL get_back
  156. cmp dh, 0
  157. je end_it ; end program
  158. CALL set_color
  159. jmp start
  160.  
  161. ;........................................................
  162.  
  163. end_it: jmp end_it
  164.  
  165.  
  166.  
  167.  
  168. MOV AX,4C00H ;End processing
  169. INT 21H
  170. MAIN ENDP ;End procedure
  171. CODESG ENDS ;End segment
  172. 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!
Henry Phillips
Web Programmer
Adirondack Area Network, LLC
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: comparing return values of subroutines

 
0
  #4
Apr 21st, 2008
Originally Posted by sickly_man View Post
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.


Originally Posted by sickly_man View Post
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!
Nope. See above.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 42
Reputation: sickly_man is an unknown quantity at this point 
Solved Threads: 0
sickly_man's Avatar
sickly_man sickly_man is offline Offline
Light Poster

Re: comparing return values of subroutines

 
0
  #5
Apr 22nd, 2008
Originally Posted by WaltP View Post
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....

  1. 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...
Henry Phillips
Web Programmer
Adirondack Area Network, LLC
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC