CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: A few questions about exception handling

    You should have posted your code within code tags. Even more than most other languages, assembly language code is particularly unreadable without them. With code tags (and after some additional shifting around to improve column alignment), your code looks like this:

    Code:
          MOV AX,
          MOV BX,
          DEC BX
          CMP AX,BX
          JC  NEXT
          JMP EXIT
    NEXT  CMP AX,0
          JG  COND1
          JMP ELSE1
    COND1 CMP BX,0
          JLE POWER
    ELSE1 DEC AX
          INC BX
          CMP AX,BX
          JC  COND2
    POWER MOV AX,BX
          MUL BX
          JMP EXIT
    COND2 CMP AX,0                  
          JNE COND3
          JMP ELSE2
    COND3 CMP BX,0      
          JNE COND4
          JMP ELSE2
    COND4 MOV AX,1
          JMP EXIT
    ELSE2 AND AX,BX
          MOV CL,3
          SHR AX,CL
          MOV BX,AX
    EXIT  HLT
    After all, I'd say you made a good start to it. However, the code still contains quite some bugs.

    The IMO most obvious one first: All your jumps are forward, so there's no loop at all in your code.

    The DEC instruction in your rendition of the first line of the C++ code should be after the CMP, since the C++ code demands post-decrement.

    The JC instruction you use for your first conditional jump is for unsigned integers while your variables are signed. (More specifically, that instruction explicitly names the flag to check, but it's technically identical to the unsigned JB instruction.) All your other conditional jumps are correct - regarding their signedness. They need a thorough re-check regarding the overall jump structure, though.

    Just to mention a few. Most of that should be relatively easy to spot by doing some debugging in your emulator. To make your code runnable in the emulator, simply give the registers initial values in the first two lines of your assembly code. The C++ code doesn't demand any specific initial values, so you may pick any values you like, but a clever choice of values may simplify spotting problems while debugging. It may not be the best choice to initialize both to 0.

    Finally, your question is completely unrelated to the subject of this thread. You should better have posted that in your other thread on that subject that already exists. Don't take the fact that you now got substantial help here as an encouragement to do further hijacking of unrelated threads. You just got that help because now you've posted your code. Please post further questions regarding this topic in the other thread. (And perhaps a moderator can move these off-topic posts to the other thread, simplifying life for us and potential readers of the threads. )
    Last edited by Eri523; May 4th, 2013 at 05:09 PM. Reason: Typos
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  2. #17
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: A few questions about exception handling

    Learn to add comments to your code and use code tags! See post #1 for example.

    How are you handling the while loop? Isn't the only time that control transfers to EXIT is JMP EXIT after the initial CMP AX,BX? In the other cases you use JMP EXIT shouldn't control jump back to your initial DEC BX? And after your SHR AX, CL MOV BX,AX?

    Assuming AX is x and BX is y, then you are corrupting the value of x as you aren't saving the value first before re-using AX eg AND AX,BX SHR AX,CL etc which alters the value of x when the c++ code doesn't.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #18
    Join Date
    Apr 2013
    Posts
    6

    Re: A few questions about exception handling

    I didn't understand what are you talk about jmp exit is this wrong ??

  4. #19
    Join Date
    Aug 2009
    Location
    Finally back in Alaska
    Posts
    141

    Re: A few questions about exception handling

    as others suggested, if youd throw it in visual studio and compile the code it would give you the disassembly. Ill tell you how to do it.

    1. Start a new project in whatever editor you use.
    2. create a function with the signature of the pow function you have listed and have it return any value you want as long as its an integer.
    3. insert your c++ code from your original post in main and set a breakpoint right at the beggining of the function.
    4.compile and run your program.
    5. when it stops on your breakpoint open the disassembly windows (in visual studio it is under the Debug--Windows menu option.

    It took me a whole 5 minutes to throw something together to do this.

    When doing this, turn off optimizations. These steps are one of the first places you should go when trying to figure something out. If you dont have/know how to get to the disassembly window in whatever debugger youre using then download visual studio express.

    as a last note, use the code tags:

    Code:
    		int pow(int a, int b){
    
    			return 65;
    
    		}
    it will make your posts easier to read

  5. #20
    Join Date
    Aug 2009
    Location
    Finally back in Alaska
    Posts
    141

    Re: A few questions about exception handling

    just an observation, you should try to limit your jumps. make your code a seqential as possible. the code you have:

    Code:
          MOV AX,
          MOV BX,
          DEC BX
          CMP AX,BX
          JC  NEXT
          JMP EXIT
    NEXT  CMP AX,0
          JG  COND1
          JMP ELSE1
    COND1 CMP BX,0
          JLE POWER
    ELSE1 DEC AX
          INC BX
          CMP AX,BX
          JC  COND2
    POWER MOV AX,BX
          MUL BX
          JMP EXIT
    COND2 CMP AX,0                  
          JNE COND3
          JMP ELSE2
    COND3 CMP BX,0      
          JNE COND4
          JMP ELSE2
    COND4 MOV AX,1
          JMP EXIT
    ELSE2 AND AX,BX
          MOV CL,3
          SHR AX,CL
          MOV BX,AX
    EXIT  HLT
    could be rewritten as:

    Code:
          MOV AX,
          MOV BX,
          DEC BX
          CMP AX,BX
          jnc EXIT
    NEXT  CMP AX,0
          jng ELSE1
    COND1 CMP BX,0
          JLE POWER
    ELSE1 DEC AX
          INC BX
          CMP AX,BX
          JC  COND2
    POWER MOV AX,BX
          MUL BX
          JMP EXIT
    COND2 CMP AX,0                  
          je ELSE2
    COND3 CMP BX,0      
          je ELSE2
    COND4 MOV AX,1
          JMP EXIT
    ELSE2 AND AX,BX
          MOV CL,3
          SHR AX,CL
          MOV BX,AX
    EXIT  HLT
    you see how you could get rid of about half the jumps. Generally speaking, you should pick the most likely path to fall through (instead of jump). I still dont understand what this code is supposed to accomplish.

  6. #21
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: A few questions about exception handling

    I still dont understand what this code is supposed to accomplish.
    Its just a homework assignment to write some assembler code! That's why we haven't provided the correct code but instead given advice as to how correct it.

    I didn't understand what are you talk about jmp exit is this wrong ??
    The only time control should transfer to EXIT is after the first CMP AX, BX (ie the terminating condition for the while loop). You need a label for the first DEC BX (WHILE??) and then elsewhere in your code where there is EXIT, it should be refer to the label WHILE, with a JMP WHILE before the final HLT statement.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #22
    Join Date
    Apr 2013
    Posts
    6

    Re: A few questions about exception handling

    Thank you for your advice about (jmps) , but i didn't know convert power (pow) function into assembly code
    i replace it in my code with square !!

  8. #23
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: A few questions about exception handling

    Quote Originally Posted by AKRichard View Post
    5. when it stops on your breakpoint open the disassembly windows (in visual studio it is under the Debug--Windows menu option.
    There also is another, IMO more straightforward way to look at the assembly language code generated by the compiler: VC++ features the compiler option /FA, which makes the compiler output the generated code as an assembly language listing file. Such a file is more informative than the debugger's disassembly view, but that is still quite useful to observe the generated code at work.

    Other compilers may offer similar options.

    However, I don't think simply handing in compiler-generated code really is the appropriate way to deal with such a homework assignment. Also, the assignment appears to demand 16-bit code which the recent VC++ versions can't generate. Looking at compiler-generated code may still be instructive regarding what code "style" the compiler generates.
    Last edited by Eri523; May 5th, 2013 at 11:24 AM. Reason: Command line option typo
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #24
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: A few questions about exception handling

    Quote Originally Posted by manarzaid View Post
    Thank you for your advice about (jmps) , but i didn't know convert power (pow) function into assembly code
    i replace it in my code with square !!
    Squaring doesn't seem to be an appropriate surrogate, already because it takes just one parameter, as opposed to the two that pow() takes. At least raising an integral base to a positive (non-negative isn't much more complicated) integral power can easily be implemented as iterative multiplication in a loop.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  10. #25
    Join Date
    Apr 2013
    Posts
    6

    Re: A few questions about exception handling

    Why the screen of the emu is empty ?????
    what can i put in the code to print the value of AX and BX??

  11. #26
    Join Date
    Aug 2009
    Location
    Finally back in Alaska
    Posts
    141

    Re: A few questions about exception handling

    There also is another, IMO more straightforward way to look at the assembly language code generated by the compiler: VC++ features the compiler option /FA, which makes the compiler output the generated code as an assembly language listing file. Such a file is more informative than the debugger's disassembly view, but that is still quite useful to observe the generated code at work.
    I allways have that option turned on just so I can browse through it once in a while.



    Why the screen of the emu is empty ?????
    what can i put in the code to print the value of AX and BX??
    I havent seen any of the examples initialize ax or bx.

  12. #27
    Join Date
    Aug 2009
    Location
    Finally back in Alaska
    Posts
    141

    Re: A few questions about exception handling

    btw, as to the original purpose of this post, Im giving up on the error handling for the time being. There is something I am just not getting about the stack frame. I have spent quite some time on the internet trying to find information on the subject, it is quite limited. Ill attempt it again when I find more on the subject.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured