0

Problem:

Write a program to convert an 8-bit binary number entered from the keyboard to the equivalent Gray code binary number, using the following the algorithm:

[broken image: http://www.harborlandings.com/images/grayAlgm.jpg]

I'm learning assembly (8086, required by class) and not sure how to do this.

Do I convert each character (1 or 0) as I receive it from the keyboard? Or, do I read in the entire 8-bit binary number, loop through it, converting as I go?

I come from Perl/Java, so visualizing how this will work in assembly is a bit daunting. Also, I'm not sure what the symbol is in the algorithm above?

Thank you for your help and critique!

Community
  • 1
  • 1
Jeremiah Burley
  • 281
  • 2
  • 4
  • 8
  • plus-in-a-circle is the exclusive or (XOR) operator. – AakashM Jun 10 '09 at 16:58
  • Are you allowed to use any sorts of libraries? Because otherwise getting input will be a real pain in the butt. – samoz Jun 10 '09 at 17:04
  • Will you run it in *old* OSes, like DOS? – Nick Dandoulakis Jun 10 '09 at 17:16
  • No, I don't think so. I've done this the following way... data segment PARLIST LABEL BYTE MAXLEN DB 40 ACTLEN DB ? inpchr DB 40 DUP(?) prmpt db '1 or 0: $', 0DH, 0AH, ends start: mov ax, data mov ds, ax mov es, ax mov bx, 1 mov cx, 0 Loop1: mov ah, 9 lea dx, prmpt int 21H ; DOS keyboard to enter character mov ah, 0AH lea dx, PARLIST int 21H ; Move input char to al mov al, inpchr cmp al, 49 ; Is input char == 1 jne Loop1 xor bx, 1 jmp Loop1 ends – Jeremiah Burley Jun 10 '09 at 17:16
  • That worked :-{ I guess no code in comments! – Jeremiah Burley Jun 10 '09 at 17:19
  • I'm running on WinXP, using emulator - http://www.emu8086.com/. – Jeremiah Burley Jun 10 '09 at 17:21

2 Answers2

2

You can simply xor the character with itself shifted right one place to get the Gray representation, no loop needed. Example when your character is in AL :

mov bl, al
shr bl, 1
xor al, bl

AL is now the Gray-code representation.

In C this would be :

c^=c>>1;

To go back to the binary representation you can xor the Gray-code with it self, shifted right by decreasing powers-of-2, starting with the largest power-of-2 which is smaller than the data size, eg:

mov bl, al
shr bl, 4
xor al, bl
mov bl, al
shr bl, 2
xor al, bl
mov bl, al
shr bl, 1
xor al, bl

In C this would be :

c^=c>>4; c^=c>>2; c^=c>>1;
matja
  • 4,014
  • 3
  • 23
  • 29
1

It has been a very long time since I have written any assembly code but the questions seems more philosophical. Without knowing the larger goals here are some ideas.

Converting every key as they are entered: Many times the program needs to respond to individual key strokes as the program is running, (ie dynamic commands, up, down, left etc.). In this case, the key strokes should be converted individually. Other times, a block of data or strings need to be converted and this operation is usually done at the conclusion of the enter key of is a larger block of data. These cases require the characters to be “looped” through and converted.

However, in either case the “work” should done in a generic subroutine that can be called from either type of situation.

I hope this helps,

Ed