如何在汇编器中检查数字是偶数还是奇数 [英] How to check if number is even or odd in Assembler

查看:166
本文介绍了如何在汇编器中检查数字是偶数还是奇数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个代码来检查数字是偶数还是奇数,但是我的程序包含一个无穷循环,所以我找不到它.

I wrote a code to check if a number is even or odd, but my program contains a endless loop and I can't really find it.

这是ISA中的所有命令:

Here are all Commands in the ISA:

命令第1部分
命令第2部分

这是我的代码:

#Read in Number
addi zero t0 1
sysmove exc t0
syscall

#Copy Number to a0
sysmove a0 I[0]

#check if number is even or odd
ldd t1 zero 0
ldd t2 zero 0
ldd t3 zero 1 #Register contains 1
ldd t4 zero 0 #Register contains 0

#divide:
subi a0 t2 2 # subtract with 2
ldd t2 a0 0 #move to a0
beq a0 t3 odd #compare a0 with 1
beq a0 t4 even #compare a0 with 0
jmp 6

#odd:
cout odd
syscall

#even: 
cout even
syscall

推荐答案

偶数还是奇数取决于最低位,即最低有效位.如果为0,则数字为偶数;否则为1,并且为奇数.

Even vs. odd is determined by the lowest bit aka Least Significant Bit.  If it is 0 then the number is even, otherwise (it is 1 and) the number is odd.

通常,可以使用具有恒定掩码1的简单AND立即操作来隔离此单个位,该操作会将除LSB之外的所有其他位清零.如果您没有AND立即数,但是具有AND,则可以将立即数1值加载到寄存器中并执行(非立即数)AND.

Normally, this single bit can be isolated using a simple AND immediate operation with constant mask 1, which would zero all the other bits except the LSB.  If you don't have an AND immediate, but have an AND, then you can load an immediate 1 value into a register and perform the (non-immediate) AND.

 1111110000000000
 5432109876543210    bit position
 ----------------
 abcdefghijklmnop    word of interest (to test for even odd)
                       note that bit "p" tells us even vs. odd
 0000000000000001
        &
 ----------------
 000000000000000p

但是,没有AND操作,还有许多其他方法可以隔离单个位(或位范围).这是一个:

Without AND operation, however, there are a number of other ways to isolate a single bit (or range of bits).  Here's one:

向左移动,直到它是剩下的唯一位.向左移动将删除较高的有效位,而向零移动.因此,如果您的计算机使用的是16位寄存器,则向左移15将产生以下值:

Shift left until it is the only remaining bit.  Shifting left will remove higher significance bits, while shifting in zeros.  Thus, if your computer is using 16-bit registers, then shifting left by 15 will result in the following value:

 abcdefghijklmnop    word of interest (to test for even odd)
                       note that bit "p" tells us even vs. odd
      << 15
 ----------------
 p000000000000000

该值可以与零进行比较.当寄存器为零时,则p为零,表示数字为偶数;当寄存器为非零时,p为非零,因此数字为奇数.

This value can be compared to zero.  When the register zero, then p is zero, meaning the number was even, and when non-zero, p is non-zero, so the number was odd.

如果只向左移一点,我们将得到

If we shifted left by only one bit we would get

 abcdefghijklmnop    word of interest (to test for even odd)
                       note that bit "p" tells us even vs. odd
      << 1
 ----------------
 bcdefghijklmnop0

因此,如果我们将同样的位移进行15倍,我们将得到 p000 ... .

So, if we do this same shift 15x we get to p000....

您应该可以使用 shli r1,r2、15 .

如果愿意,可以采用该左移的值并将其右移.算术右移,结果将是:

If you like you can take that left-shifted value and shift it right.  With an arithmetic right shift the result will be:

p000000000000000
       >> 15 arithmetic shift
----------------
pppppppppppppppp

算术移位适用于提取带符号的字段.(当然,我们很少看到带符号的字段,因为1的带符号的字段只能代表-1和0!)

The arithmetic shift is appropriate for extracting signed fields.  (We rarely see a single bit signed field, of course, as 1 bit signed field can only represent -1 and 0!)

通过逻辑右移,结果将是:

And with a logical right shift the result will be:

p000000000000000
       >> 15 logical shift
----------------
000000000000000p

此结果将低位恢复到原来的位置(并给出与AND相同的答案,立即数为1).(但是,我们不需要将 p 位返回到其原始位置只是为了将其与0进行比较.)

This result restores the low bit to its former position (and gives the same answer as AND with an immediate of 1).  (However, we don't need to return the bit p to its original position just to test it against 0.)

如果您希望进行无损平移,则很容易获得旋转指令.在某些情况下,由于无损移位不需要额外的寄存器来保留原始位,因此它们在CPU寄存器不足的机器上特别有用.但是对于提取位或字段,我们需要进行有损操作,以释放或剥离位,因此移位可以完成工作.

The rotate instructions are handy to have if you want a loss-less shift.  These are particularly useful on machines that are poor in CPU registers, since the loss-less shift doesn't require an additional register to preserve original bits, in some scenarios.  But for extraction of a bit or field, we need a lossy operation, to loose or strip away bits, so the shifts do the job.

这篇关于如何在汇编器中检查数字是偶数还是奇数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆