13 Replies - 9383 Views - Last Post: 25 October 2012 - 07:35 PM

#1 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 07:00 AM

Hi all,
Looking for a little guidance on an assignment I am working on. The assignment is to prompt the user to enter a number, and then print out the 32 bit binary digits of that decimal number using WriteChar.

The desired output is:
Enter the number:5
00000000000000000000000000000101
Enter the number:-1
11111111111111111111111111111111
etc...

I have tried a few different things but not quite sure how to get to the end result.
Here is where I am at:
INCLUDE Irvine32.inc
.data
line BYTE 0dh, 0ah,0
myString BYTE "Enter the number: ", 0
intVal SDWORD ?

.code
main PROC

	mov edx, OFFSET myString
	call WriteString ;prompt the user to enter a number
	mov edx, OFFSET line
	call WriteString ;new line
	call ReadInt ;read the number entered by user
	mov intVal, eax
	call DumpRegs ;to see the values
	call WriteChar
	
	exit
main ENDP
END main



From my book, it looks like WriteChar is writing whatever is in AL, to the screen. Al being an 8 bit register that is the lower portion of AX, which is part of EAX. I am not sure how to use WriteChar now to get at the 32 bit value in eax. I know I need to use a recursive function or loop with WriteChar, then shifting left or right, and WriteChar again, etc... to get the full 32 bit binary value.

I have it copied into the variable intVal in case I need to use eax for something else, but anyways. Any help you can provide is appreciated.


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Assembly Language - Decimal to Binary using WriteChar

#2 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 01:39 PM

Worked on this a little more. I need to use the compare instruction cmp al, 00000001b to see if the last bit is a 0 or 1, based on the zero flag, then shift left 1 and try again

What is the trick to going through this only 32 times (to get all the bits)?

Here is my updated code:
INCLUDE Irvine32.inc

.data
line BYTE 0dh, 0ah,0
myString BYTE "Enter the number: ", 0
intVal SDWORD ?
len = (32)

.code
main PROC

	mov edx, OFFSET myString
	call WriteString		;prompt user for number
	call ReadInt			;read number from user
	mov edx, OFFSET line
	call WriteString
	mov intVal, eax			;copy user number into variable intVal
	cmp al, 00000001b
	jz L1
	jnz L2

	L1:
		mov al, "0"
		call WriteChar
		SHL al, 1
		loop L1
	
	L2:
		mov al, "1"
		call WriteChar
		SHL al, 1
		loop L2
			
	exit
main ENDP
END main



Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 04:07 PM

It is a bit more involved than that. You can get a hint and see how Kip does it by opening up Irvine32.asm and looking at WriteBinB
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 04:52 PM

Ok, here is one way to do it using Irvine:
.code
main:
    push    ebp
    mov     ebp, esp
    sub     esp, 36
    push    esi
    push    edi
    
	mov     edx, OFFSET myString
	call    WriteString		;prompt user for number
	
	call    ReadInt			;read number from user
	mov     intVal, eax
	mov     edx, OFFSET line
	call    WriteString
	
	; ###################################
	mov     esi, 31                     ; count 32 bytes
    lea     edi, [ebp - 36]             ; pointer to buffer to hold ASCII binary number

@@:
	ror     intVal, 1                   ; rotate next bit into carry flag
	jc      L2                          ; if 1
	
    mov     byte ptr [edi + esi], "0"   ; move ASCII 0 into buffer [pointer + counter]
    dec     esi                         ; decrease our counter
    js      @F                          ; if counter is signed, we are done
    jmp     @B                          ; get next bit
	
L2:
	mov     byte ptr [edi + esi], "1"   ; move ASCII 1 into buffer [pointer + counter]
	dec     esi
	js      @F
	jmp     @B

@@:		
	lea     edx, [ebp - 36]             ; Print our buffer
	call    WriteString
	
	mov     edx, offset line
	call    WriteString
	
	call    WaitMsg
	
	pop     edi
	pop     esi
	add     esp, 36
	mov     esp, ebp
	pop     ebp
	exit

END main

Was This Post Helpful? 1
  • +
  • -

#5 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 05:19 PM

Thanks Gunner. I compiled what you have there and it gets the job done. Now I will just have to spend some time figuring out how you did it as some of the things you have in there are more advanced then what I have seen so far in this class.
Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 05:20 PM

I was not about to give you something that easy to submit... You can learn from it, and maybe modify it.
Was This Post Helpful? 0
  • +
  • -

#7 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 05:22 PM

Totally fine, as I need to know how to do it for the tests anyways.
Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 05:32 PM

Let me explain it a bit:
sub esp, 36 makes room on the stack for a buffer to hold our 32 bits + 1 for a NULL, the other 3 are for alignment.

Now we load the address of our buffer into edi. Now, let's say our stack pointer is 1638280 and we subtract 32 from it, our pointer to the buffer will be 1638244, right? Now we are using esi as the index into our buffer, so we start at edi + 31 which our first character will be inserted at address 1638275, decrease our counter, next character will be at 1638274, and next one will be at 1638273 etc... kind of make sense?
Was This Post Helpful? 1
  • +
  • -

#9 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 06:47 PM

Yes, that part makes sense. I am not totally sure how to loop through though. Looks like you are using ESI at 31, and dec'ing it each time through your loop?
Was This Post Helpful? 0
  • +
  • -

#10 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 06:52 PM

Yes, there are 32 bits and we start our counter at 31. 0 - 31 bits. Once the counter hits -1 the loop is done. We also use this as our index into our buffer pointer. DEC sets some flags, so we check to see if the SIGN flag is set and if it is we know our loop is done. Couldn't come up with label names, so I used Anonymous labels.
Was This Post Helpful? 0
  • +
  • -

#11 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 07:13 PM

Is there a difference between using ROR and SHR?
Was This Post Helpful? 0
  • +
  • -

#12 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 07:20 PM

And you said you couldn't think of names so you used anonymous labels, is that what @F and @B are, just different anonymous labels?

For example, where does jmp @B send the program? Like jc L2 would jump to L2 if the carry flag was 1, but I am not sure where the jmp @B points.
Was This Post Helpful? 0
  • +
  • -

#13 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 07:30 PM

Well, yes there is. I used ROR in that example so after you rotate 31 times, you get back the original number.

ROR - rotates the bits to the right and carries them over bit 0 becomes bit 31
SHR - does the same, but discards the bit

so 1101001 ROR becomes 10000000000000000000000000110100
1101001 SHR becomes 110100

You can see what ROR, ROL, OR, XOR, SHL, SHR, NOT, AND, all do to bits in Windows Calc, open calc, and press ALT + 3, this will switch to programmer view. You can see the bits of numbers and the results of operations.

@F goes to the next @@ label, @B goes to the previous @@

@@:
Do something
jmp @F - goes to something else

@@:
Something else
jmp @F - goes there
jmp @B - goes do something

@@:
there
jmp @B - goes to something else
Was This Post Helpful? 0
  • +
  • -

#14 Kraymerica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 25-October 12

Re: Assembly Language - Decimal to Binary using WriteChar

Posted 25 October 2012 - 07:35 PM

Ahh, ok that makes sense. Thanks Gunner, you've been very helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1