Author Topic: Outputting Characters based on a binary input  (Read 6090 times)

Offline Panthon

  • New Member
  • Posts: 1
Outputting Characters based on a binary input
« on: March 16, 2011, 09:48:51 PM »
I recently had to start using NASM as part of course on Computer orginization and I am hopelessly lost.

How do I output to the screen a set of characters based on a binary input?
E.G: Input: 10010010 (read in from an array),
      Output *##*##*# (onto the screen).
 
I need to be able to do this for different arrays, * in place of 1's and # in place of 0's depending on the values in the arrays.

I've got the array defined but I'm stumped as to how to compare each bit in the word, and output a different character based on the different states.

Any suggestions would be greatly appreciated.

Thanks

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Outputting Characters based on a binary input
« Reply #1 on: March 18, 2011, 02:13:33 PM »
How is this "array" defined?

Code: [Select]
my_text_array db "10101001"
my_byte_array db 1, 3, 5, 7
my_dword_array dd 1, 3, 5, 7

If it's a "text" array, each '1' or '0' will be a byte, and all you have to do is change 'em to '*' and '#'... You're probably not that lucky.

Otherwise, it's essentially the same task as displaying the number(s) in binary, except you'll be using '*' and '#' instead of '0' and '1' - '0' and '1' (the ascii codes) are contiguous, and '*' and '#' are not. In either case, you'll need to isolate the bits (I guess that's what you're asking, re-reading the question)...

If we shift a value left or right, the bit that's shifted out winds up in the carry flag. For '0' and '1', we can start with '0' and "adc al, 0" to increment it to a '1' if the carry was set. That won't work for you, you'll have to "jc" or "jnc"...

Code: [Select]
   mov esi, my_byte_array

do_bytes:
; get a byte
   mov al, [esi]
   inc esi
; (or "lodsb" will do both}

   mov ecx, 8 ; 8 (16, 32?) bits to do
do_bits:
; process 8 bits
   shl al, 1 ; high bit into carry
   jc not_zero
; print a '*'
   jmp next_bit
not_zero:
; print a '#'
next_bit:
   loop do_bits
; are we at end of array? if not...
   jmp do_bytes
; else "ret" or exit cleanly

Something along those lines? (untested!) Why don't you show us what you've got so far? That may clear up the "What OS?" question, too...

Best,
Frank


Offline huggles

  • New Member
  • Posts: 1
Re: Outputting Characters based on a binary input
« Reply #2 on: March 24, 2011, 06:44:47 AM »
Hey guys,

I have also just started with NASM, have been coding in java for about 10 years now, still trying to get my head around all the new bits and pieces.

I have a similar problem, in that I am thinking of using integer numbers to store my bit patterns.

Bit pattern = 10110011
Integer = 179

I am thinking that if I keep shifting the integer to the left, the carry flag will hold the current bit. I can then decide if the bit is 1, print '#' and if the bit is 0 print '*'

I am not sure how to work with a list of such masks, for example if I had

00110000 = 48
01001000 = 72
11111000 = 248
10001000 = 136
10001000 = 136

for each of the 5 numbers, 48, 72, 248, 136, 136, I would like to put the value in a register, and then shift the register until there are no more bits, each time checking the carry flag for it's value and then deciding what to print.

Any help / criticism would be greatly appreciated.

h

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Outputting Characters based on a binary input
« Reply #3 on: March 24, 2011, 04:02:23 PM »

There are multiple ways to solve this problem.
Firstly, let's establish the limits for your numbers.  From your example there is no number greater than 255 thus you can use 8 bits for number storage.  If you have numbers that are in excess of 255 then determine how many bits max you need to hold.  I'll assume 8 bits is enough here.

The following is NOT tested code - just ONE example idea ( of many ) for you to implement and test:

Code: [Select]
   ; how many bits to test?
   mov ecx, 8

   ;Store number in 8-register
   mov bl, Your8BitNumber

again:   ; the loop label

   ; get shifted number
   mov al, bl

   ; Logically AND the number and mask
   and eax, 0x0080

   ; Pseudocode to test register contents and print desired output
   if register > 0
       Print #
   else
       Print *

   ; Shift register bit contents left 1 position
   shl

   ; do again if count in cx > 0
   loopnz again    ; this will dec register ecx and fall through if it's zero
   .
   ; done
   .

You will definitely need to expand upon this ( including using the correct syntax ) as I purposely did not write your code for you.
However, this should at least point you in the right direction.

Also, just an FYI - this board is more for NASM related questions rather than assembly programming in general.
You may wish to visit http://www.asmcommunity.net to read and post questions of a more general nature there.