Thread: Procedure

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    3

    Procedure

    If an assembly language procedure is called from a C program and the procedure writes a new value into the stack segment register, what will happen when the procedure returns to the C program?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The assembly function must save the value of that register before changing it and restore it before returning. If it doesn't, then the program and possibly the operating system will go to never, never land because the return address is on the stack.
    Last edited by Ancient Dragon; 11-01-2005 at 10:19 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    Sometimes things seem so simple in hindsight. Thanks for the reply.

    So what would happen if a procedure writes a new value into the code segment register?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Brandy22
    Sometimes things seem so simple in hindsight. Thanks for the reply.

    So what would happen if a procedure writes a new value into the code segment register?
    People who do not know assembly language should not be fiddling around with those register values. The code segment tells the computer where to look for code instructions. Writing random crap to registers WILL crash the program, possibly the operating system, and possibly even destroy the computer's hardware such as the hard drive. Actual results are unpredictable.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    3
    So basically if the procedure writes a new value into the code segment it will cause the code segment to look to a different instruction?

    Thanks for all your help

  6. #6
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by Brandy22
    So basically if the procedure writes a new value into the code segment it will cause the code segment to look to a different instruction?

    Thanks for all your help
    Code segment is read-only .you cant change the content of code segment.When any procedure is called the value of Program counter(a register in cpu,PC) is stored on stack.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by cbastard
    Code segment is read-only .you cant change the content of code segment.When any procedure is called the value of Program counter(a register in cpu,PC) is stored on stack.
    I think he meant the CS register, not the contents of the segment. But even the contents of the segment may be writible on some operating systems, such as MS-DOS 6.X and earlier. That's exactly what happens when function int86() is called -- it changes some instructions in the code segment then executes them.
    Last edited by Ancient Dragon; 11-01-2005 at 04:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hooked Procedure Early Return In/External Trigger
    By n00b3 in forum Windows Programming
    Replies: 4
    Last Post: 06-29-2008, 05:37 PM
  2. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  3. Replies: 0
    Last Post: 04-21-2006, 01:41 PM
  4. Window Procedure Question
    By shinobisot in forum Windows Programming
    Replies: 3
    Last Post: 01-15-2003, 11:38 AM
  5. Collision with quads?
    By SyntaxBubble in forum Game Programming
    Replies: 6
    Last Post: 01-18-2002, 06:17 PM