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. :thumb: 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. :))
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.
Re: A few questions about exception handling
I didn't understand what are you talk about jmp exit is this wrong ??
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
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.
Re: A few questions about exception handling
Quote:
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.
Quote:
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.
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 !!
Re: A few questions about exception handling
Quote:
Originally Posted by
AKRichard
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.
Re: A few questions about exception handling
Quote:
Originally Posted by
manarzaid
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.
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??
Re: A few questions about exception handling
Quote:
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.
Quote:
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.
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.