PDA

View Full Version : Bit-Shift to the Right [?]

Memo
04-23-2002, 06:41 PM
Ahh. I've been struggling on this for a while now. I was wondering if anyone could help me. I am supposed to shift bits to the right and left in assembly code. The problem is the assembly language doesn't have bit shift operators. I only have NOT, AND, ADD available to me. Shifting to the left is easy, just add the number to itself. Does anyone have any idea of how to shift to the right using only these 3 operations? NOT (inverts the bits), AND and ADD?

cruelpupet
04-23-2002, 07:18 PM
Originally posted by UT Memo
Ahh. I've been struggling on this for a while now. I was wondering if anyone could help me. I am supposed to shift bits to the right and left in assembly code. The problem is the assembly language doesn't have bit shift operators. I only have NOT, AND, ADD available to me. Shifting to the left is easy, just add the number to itself. Does anyone have any idea of how to shift to the right using only these 3 operations? NOT (inverts the bits), AND and ADD?

MASM has bit shift operators

For basic shifting
SHL
SHR

For rotating
ROL
ROR

Memo
04-23-2002, 07:22 PM
Ya...cept we're using sort of an ISA simulator called the LC-2

cruelpupet
04-23-2002, 07:29 PM

eSDee
04-23-2002, 08:07 PM

hapoo
04-23-2002, 08:09 PM
Originally posted by EsDeeLoco

wouldn't that give you straight ones?

_10101100
+01010011
=11111111

Memo
04-23-2002, 08:13 PM
The inverse would always give me -1 since all numbers are represented in 2's Compliment.

for example:

01100
10011 +
--------
11111

11001
00110 +
--------
11111

right?

Basically what I have to do is divide the number in half but without am division function that's a little tough. There's supposed to be a way to do it with just AND, NOT, ADD :-/

StonedWheat
04-23-2002, 08:35 PM
I think it's impossible. Shifting left is easy cus it's just multiplying by two, so adding the same value gives you the answer. Shifting right is like dividing by two. If you can figure out how to add the negative of half its value, then you can do it. However, that probably requires a right shift :banghead: I can't think of any other way.

Kevster
04-23-2002, 08:50 PM
I used remember how to do this and it's in my old Digital Logic notes at home. I'll look it up for you tonight when I get home and post it then.

Kevster
04-23-2002, 09:33 PM
Ok - things are coming back to me here. I haven't gone home yet but I do remember that doing a bit shift to the right involves dividing the number by 2, which is essentially successive subtraction.

Now if I can just remember how to do subtraction using NOT, AND and ADD - then you've got your solution.

EDIT: Sorry StonedWheat, I just saw your post above. You said the same thing.

StonedWheat
04-24-2002, 03:26 PM
Originally posted by Kevster
Ok - things are coming back to me here. I haven't gone home yet but I do remember that doing a bit shift to the right involves dividing the number by 2, which is essentially successive subtraction.

Now if I can just remember how to do subtraction using NOT, AND and ADD - then you've got your solution.

EDIT: Sorry StonedWheat, I just saw your post above. You said the same thing.

You get the negative of a number by taking the 2's complement and adding 1. For example to get -3, you take the two's complement of 011 which is 100, and add 1 to get 101.

101
+101
____
010

so 5 + (-3) = 2

I still can't figure out how to subract half of the number from itself. Did you get it yet UT Memo?