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

4

0 に答える 0