1

EDIT: Sign extension fixed my problems with divide and modulus, and a silly typo fixed my or. My Factorial does display my wanted 0 with 0x0...0 but I'm still confused on how to output that one sentence before the factorial result. I deleted most of the unnecessary code concerning factorial. Basically, how do I print a string in assembly? Google is not my friend today

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char message[] = "Factorial input must be a positive integer >=1";

int calc (int op1, int op2, int opcode)
{
__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       ;check for subtract
jne mul_3           ;
mov eax, op1        ;
sub eax, op2        ;
jmp done            ; 

mul_3:
cmp ebx, 0x03       ;check for multiply
jne div_4           ;
mov eax, op1        ;
mul op2             ; 
jmp done            ;

div_4:
cmp ebx, 0x04       ;check for divide
jne mod_5
xor edx, edx        ; zero out the register
mov eax, op1
div op2
jmp done

mod_5:
cmp ebx, 0x05       ;check for modulus
jne and_6
xor edx, edx        ; zero out the register
mov eax, op1
div op2
mov eax, edx
jmp done

and_6:
cmp ebx, 0x06       ;check for &
jne or_7
xor edx, edx        ; zero out the register
mov eax, op1
and eax, op2
jmp done

or_7:
cmp ebx, 0x07        ;check for |
jne xor_8
xor edx, edx         ; zero out the register
mov eax, op1
xor eax, op2
jmp done

xor_8:
cmp ebx, 0x08       ;check for xor
jne fac_9
xor edx, edx        ; zero out the register
mov eax, op1
xor eax, op2
jmp done

fac_9:
cmp ebx, 0x09       ;check for !
jne done;
xor edx, edx        ; zero out the register
mov eax, op1
cmp eax, 0
jl error
wp1:
mov ecx, op1
DEC ecx
mov ebx, 1
L1:
mul ebx
INC ebx
LOOP L1
jmp done

error:
    mov eah, 9h            ;no clue how to
    lea eax, message       ;print a message in assembly
    jmp wp1

done:
};
}

I'm having a few problems with negative input. When I have negative factorial for op1, it just crashes and burns when it's supposed to print out the message "Factorial input...>=1" and then just returns a 0. Also with negative input my divide, modulus, and or results are wrong.

Here's what I am supposed to get

Operand 1 = -10    Operand 2 = -5
Add:        -15   xfffffff1
Sub:         -5   xfffffffb
Mul:         50   x00000032
Div:          2   x00000002
Mod:          0   x00000000
And:        -14   xfffffff2
Or:          -1   xffffffff
Xor:         13   x0000000d
Error: Factorial input must be a positive integer >=1
Fac:          0   x00000000

and what I do get.

Operand 1 = -10    Operand 2 = -5
Add:        -15   xfffffff1
Sub:         -5   xfffffffb
Mul:         50   x00000032
Div:          0   x00000000
Mod:        -10   xfffffff6
And:        -14   xfffffff2
Or:          13   x0000000d
Xor:         13   x0000000d
**crashes**

Any help? I'm using Visual Studio c++ in x86

gooberdope
  • 75
  • 1
  • 3
  • 13
  • 2
    This isn't an answer to the whole question, but you need to sign-extend into `edx` rather than just zero it. See: http://stackoverflow.com/a/10348927/922184 – Mysticial Jul 07 '12 at 17:51
  • loose one of your tags and and `c++` instead – Chris Moutray Jul 07 '12 at 17:54
  • 2
    You've got a debugger. Why can't you just step through your code and identify your problem and its source more precisely? Come on, why should we debug for you or interpret the code in the head when you can just debug it yourself? – Alexey Frunze Jul 07 '12 at 17:56
  • I would write a wrapper function (in C++) around `calc` that would verify arguments before calling the assembly routine. – jxh Jul 07 '12 at 18:13
  • It's meant to be a signed `div` - don't zero the upper half, sign extend it. Same for `mod`. The `or` is wrong because there is no `or`, there two `xor`'s. – harold Jul 07 '12 at 18:42
  • The code for factorial doesn't make enough sense for me to do much with it, and notice that `error` jumps right back into the factorial. – harold Jul 07 '12 at 18:47
  • Well for the factorial, if I have a negative number, I'm supposed to just display the message (which i have no idea how to do), then somehow display Fac: 0 x00000000 – gooberdope Jul 07 '12 at 18:50
  • You really can't expect us to debug all that assembler code for you. You do know we're not getting paid? – TonyK Jul 07 '12 at 19:31
  • @TonyK I'm not really asking anyone to debug. Also add the fact that the only real problem I'm having is how to print a string in assembly because google is not my friend today and the more I research the more I get confused. Most of the explanations that I found online and tried just gave me more errors. If someone could lead me to a working explanation or detailed, that's good enough for me. – gooberdope Jul 07 '12 at 19:40
  • 2
    OK. How to print a string in assembly: Don't. Just don't. – TonyK Jul 07 '12 at 20:09

0 Answers0