简体   繁体   English

MUL和XOR组装说明

[英]MUL and XOR Assembly Instructions

inb4: yes I've seen the similar question with the same assignment posted before however mine is slightly different. inb4:是的,我之前看到过类似的问题,但分配的任务相同,但是我的略有不同。 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和XOR,我已经完成了大多数功能。 MUL is implemented however to receive full credit we need to implement it without using the actual MUL operation. 实现了MUL,但是要获得全部信用,我们需要在不使用实际MUL操作的情况下实现它。 Loop? 环? For the XOR function I'm simply getting slightly off outputs. 对于XOR函数,我只是略微降低了输出。 Registers not cleared? 寄存器未清除? This is my second week programming in intel assembly so I am pretty much clueless at this point. 这是我第二周在intel汇编中进行编程,所以到此为止我几乎一无所知。 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. 谢谢这是C函数中的汇编代码,需要一个运算符和两个整数。

__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:
}

MUL UL

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;

PS 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 PS自从我真正编写了asm以来已经过去了几十年,所以这可能不是最好的解决方案,但是您要求的是一些简单的方法

EDIT: Fixed MUL to work with signed integers 编辑:固定MUL与带符号的整数一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM