inline assembly...coz that's how we roll.
Code:
#include <iostream>
using namespace std;
void main() {
int myGrade = 75;
_asm
{
mov eax, dword ptr [myGrade]
mov ecx,'A'
cmp eax,89
setle ch
add cl,ch
cmp eax,79
setle ch
add cl,ch
cmp eax,69
setle ch
add cl,ch
cmp eax,59
setle ch
add cl,ch
add cl,ch
xor ch,ch
mov dword ptr [myGrade],ecx
}
cout<<(char)myGrade<<endl;
}
For the life of me I couldn't figure out how to "cout" in assembly or lord knows I'd have put that in there too.
Seems like there should be a way to do it with 1 less jump, but I'm too sleepy to figure that out.
as a comparison, that above nested comparison method expands to this in assembly:
Code:
myGrade=(myGrade>89?'A':myGrade>79?'B':myGrade>69?'C':myGrade>59?'D':'F');
00401D31 cmp dword ptr [myGrade],59h
00401D35 jle main+7Dh (401D3Dh)
00401D37 mov byte ptr [ebp-0Dh],41h
00401D3B jmp main+0AFh (401D6Fh)
00401D3D cmp dword ptr [myGrade],4Fh
00401D41 jle main+89h (401D49h)
00401D43 mov byte ptr [ebp-0Eh],42h
00401D47 jmp main+0A9h (401D69h)
00401D49 cmp dword ptr [myGrade],45h
00401D4D jle main+95h (401D55h)
00401D4F mov byte ptr [ebp-0Fh],43h
00401D53 jmp main+0A3h (401D63h)
00401D55 cmp dword ptr [myGrade],3Bh
00401D59 setle dl
00401D5C lea edx,[edx+edx+44h]
00401D60 mov byte ptr [ebp-0Fh],dl
00401D63 mov al,byte ptr [ebp-0Fh]
00401D66 mov byte ptr [ebp-0Eh],al
00401D69 mov cl,byte ptr [ebp-0Eh]
00401D6C mov byte ptr [ebp-0Dh],cl
00401D6F movsx edx,byte ptr [ebp-0Dh]
00401D73 mov dword ptr [myGrade],edx
Bleh. I figured out what it was doing after the setle dl line. Seems overly complex to me, but what do I know.
00401D59 setle dl -> if my grade was less than 59 this will be 1
00401D5C lea edx,[edx+edx+44h] -> this adds 1+1 to 'D' based on the above and stores it in edx, of which the low order byte is used next (dl)
the rest of the lines round-robin the values up, unwinding the nested comparisons until the final result gets put in myGrade. To be honest I have absolutely no idea why this unwinding was neccesary. Seems like it would have been faster just to push the grade into eax or ebx or something and just leave it there. Guessing there's some way eax, ecx, or edx could have been reused in some optimized way in the equation.
I hate myself for spending 2 extra hours at work figuring this out.
edit: Turns out the original asm I posted didn't work quiiiiet right. This one works but isn't as nice. 
Will spend more time on it just because...