Reputation: 43
inb4: yes I've seen the similar question with the same assignment posted before however mine is slightly different. Okay so here's the coding assignment, create a basic calculator. I've completed most of the functions except for two: MUL and XOR. MUL is implemented however to receive full credit we need to implement it without using the actual MUL operation. Loop? For the XOR function I'm simply getting slightly off outputs. Registers not cleared? This is my second week programming in intel assembly so I am pretty much clueless at this point. No idea how to implement a loop so some solid help would be great. Thanks This is the assembly code in a C function that takes an operator and two integers.
__asm
{
mov eax, 0; zero out the result
mov ebx, opcode; move opcode to ebx for comparison
cmp ebx, 0x01; check for ADD
jne sub_2;
mov eax, op1;
add eax, op2;
jmp done;
sub_2:
cmp ebx, 0x02;
jne mul_3;
mov eax, op1;
sub eax, op2;
jmp done;
mul_3:
cmp ebx, 0x03;
jne div_4;
mov eax, op1;
mul op2; //NOT supposed to use MUL
jmp done;
div_4:
cmp ebx, 0x04;
jne mod_5;
mov eax, op1;
cdq; //32bit number turns into 64bit for eax
idiv op2;
jmp done;
mod_5:
cmp ebx, 0x05;
jne and_6;
xor edx, edx; //Clear edx;
mov eax, op1;
cdq; //32bit to 64 for eax;
idiv op2;
mov eax, edx;
jmp done;
and_6:
cmp ebx, 0x06;
jne or_7;
xor edx, edx; //Clear edx
mov eax, op1;
and eax, op2;
jmp done;
or_7:
cmp ebx, 0x07;
jne xor_8;
xor edx, edx; //Clear edx
mov eax, op1;
xor eax, op2;
jmp done;
xor_8:
cmp ebx, 0x08;
jne fac_9;
xor edx, edx; //Clear edx
mov edx, op1;
xor eax, op2;
jmp done;
fac_9:
cmp ebx, 0x09;
jne done;
xor edx, edx; zero out the register
mov eax, op1;
cmp eax, 0;
mov ecx, op1;
DEC ecx;
mov ebx, 1;
L1:
mul ebx;
INC ebx;
LOOP L1;
jmp done;
done:
}
Upvotes: 0
Views: 856
Reputation: 46341
MUL
mul_3:
cmp ebx, 0x03;
jne div_4;
mov edx, op1;
mov eax, 0;
mov ecx, op2;
test ecx, ecx;
jz done;
jns mul_add;
neg ecx;
neg edx;
mul_add:
add eax, edx;
loop mul_add;
jmp done;
XOR
xor_8:
cmp ebx, 0x08;
jne fac_9;
xor edx, edx; //Clear edx
mov edx, op1;
mov eax, op2;
xor eax, edx;
jmp done;
P.S. It's been a couple of decades since I really coded asm, so this is probably not the best solution, but you asked for something simple
EDIT: Fixed MUL to work with signed integers
Upvotes: 2