Hey! I need some serious help with this project. I must convert this c++ code to
ID: 3533440 • Letter: H
Question
Hey! I need some serious help with this project. I must convert this c++ code to pep/8 assembly language and im stuck on calling the doMath function, as well as with the case statements inside the doMath function. I Havent gotton to the divide and multiply function yet because I want to make sure the doMath function works before i add in the others that are called by the doMath.
Here's the c++ code
BR main
result: .EQUATE 5 ;local variable #2d ;int result,
num1: .EQUATE 3 ;local variable #2d ; num1,
num2: .EQUATE 1 ;local variable #2d ; num2,
op: .EQUATE 0 ;local variable #1c ; op;
;*********************************************** int divide(int dvnd, int dvr)
;divide:
;*********************************************** multiply(int mcand, int mpr)
;multiply:
;*********************************************** int doMath(int num1, int num2, char op)
d_retVal: .EQUATE 7 ;returned value #2d
n1: .EQUATE 5 ;formal parameter #2d
n2: .EQUATE 3 ;formal parameter #2d
d_op: .EQUATE 2 ;formal paramater #1c
val: .EQUATE 0 ;local variable #2d
;
doMath: SUBSP 2, i ;allocate #val ;int val;
LDX op, s ;switch(op)
ASLX
BR opSwitch, x
opSwitch: .ADDRSS casePlus
.ADDRSS caseMin
.ADDRSS caseMult
.ADDRSS caseDiv
casePlus: LDA num1, s ;case '+':
ADDA num2, s ;val = num1 + num2;
STA val,s
BR endCase ;break;
caseMin: LDA num1, s ;case '-'
SUBA num2, s ;val = num1 - num2
STA val, s
BR endCase ;break
caseMult: BR endCase
caseDiv: BR endCase
endCase: SUBSP 2, i ;deallocate #val
RET0
;************************************************** int main()***********************************************************************************
main: SUBSP 7, i ;allocate #result #num1 #num2 #op
BR m_endFor ;for(;;){
m_for: CHARO ' ', i ;cout << endl
STRO m_msg1, d ; << "Enter first number: ";
DECI num1, s ;cin >> num1;
CHARO ' ', i ;cout << endl
STRO m_msg2, d ; << "Enter second number: ";
DECI num2, s ;cin >> num2;
CHARO ' ', i ;cout << endl
STRO m_msg3, d ; << "Enter operation (+, -, *, /, q (quit)): ";
CHARI op, s ;cin >> op;
m_if: LDA op, s ;if( op == 'q')
CPA op, s
; BREQ m_endIf
LDA num1, s ;get num1 into Acc
STA -2, s ;push num1
LDA num2, s ;get num2 into Acc
STA -4, s ;push num2
LDA op, s ;get op into Acc
STA -5, s ;push op
SUBSP 5, i ;push #num1 #num2 #op
CALL doMath ;result = doMath(num1, num2, op)
ADDSP 5, i ;pop #op #num2 #num1
ADDSP 7, i ;deallocate #op #num2 #num1 #result
m_endIf: STOP
m_endFor: BR m_for
m_msg1: .ASCII "Enter first number: "
m_msg2: .ASCII "Enter second number: "
m_msg3: .ASCII "Enter operation (+, -, *, /, q (quit)): ;"
STOP
.END
Explanation / Answer
I've never worked with PEP/8. but I translated your doMath function to intel x86 assembly language so maybe you can absract off this:
00DE16D0 push ebp
00DE16D1 mov ebp,esp
00DE16D3 sub esp,0D8h
00DE16D9 push ebx
00DE16DA push esi
00DE16DB push edi
00DE16DC lea edi,[ebp-0D8h]
00DE16E2 mov ecx,36h
00DE16E7 mov eax,0CCCCCCCCh
00DE16EC rep stos dword ptr es:[edi]
00DE16EE mov byte ptr [ebp-0D1h],0
int val;
if (op == '+')
00DE16F5 movsx eax,byte ptr [op]
00DE16F9 cmp eax,2Bh
00DE16FC jne doMath+40h (0DE1710h)
val = num1 + num2;
00DE16FE mov eax,dword ptr [num1]
00DE1701 add eax,dword ptr [num2]
00DE1704 mov byte ptr [ebp-0D1h],1
00DE170B mov dword ptr [val],eax
00DE170E jmp doMath+0A3h (0DE1773h)
else if (op == '-')
00DE1710 movsx eax,byte ptr [op]
00DE1714 cmp eax,2Dh
00DE1717 jne doMath+5Bh (0DE172Bh)
val = num1 - num2;
00DE1719 mov eax,dword ptr [num1]
00DE171C sub eax,dword ptr [num2]
00DE171F mov byte ptr [ebp-0D1h],1
00DE1726 mov dword ptr [val],eax
00DE1729 jmp doMath+0A3h (0DE1773h)
else if (op == '*')
00DE172B movsx eax,byte ptr [op]
00DE172F cmp eax,2Ah
00DE1732 jne doMath+80h (0DE1750h)
val = multiply(num1, num2);
00DE1734 mov eax,dword ptr [num2]
00DE1737 push eax
00DE1738 mov ecx,dword ptr [num1]
00DE173B push ecx
00DE173C call multiply (0DE11FEh)
00DE1741 add esp,8
00DE1744 mov byte ptr [ebp-0D1h],1
00DE174B mov dword ptr [val],eax
00DE174E jmp doMath+0A3h (0DE1773h)
else if (op == '/')
00DE1750 movsx eax,byte ptr [op]
00DE1754 cmp eax,2Fh
00DE1757 jne doMath+0A3h (0DE1773h)
val = divide(num1, num2);
00DE1759 mov eax,dword ptr [num2]
00DE175C push eax
00DE175D mov ecx,dword ptr [num1]
00DE1760 push ecx
00DE1761 call divide (0DE10CDh)
00DE1766 add esp,8
00DE1769 mov byte ptr [ebp-0D1h],1
00DE1770 mov dword ptr [val],eax
return val;
00DE1773 cmp byte ptr [ebp-0D1h],0
00DE177A jne doMath+0B9h (0DE1789h)
00DE177C push offset (0DE17A0h)
00DE1781 call @ILT+260(__RTC_UninitUse) (0DE1109h) <- ignore this
00DE1786 add esp,4
00DE1789 mov eax,dword ptr [val]
}
00DE178C pop edi
00DE178D pop esi
00DE178E pop ebx
00DE178F add esp,0D8h
00DE1795 cmp ebp,esp
00DE1797 call @ILT+455(__RTC_CheckEsp) (0DE11CCh) <- ignore this
00DE179C mov esp,ebp
00DE179E pop ebp