Binary Function

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
feare56
Member
Member
Posts: 97
Joined: Sun Dec 23, 2012 5:48 pm

Binary Function

Post by feare56 »

I had this idea where you would type in a number with the command 'bin' to output the chosen number in binary for example 'bin 57'. How would I implement this with assembly? Also would this make developing easier later on or would it be just a "fun" command
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Binary Function

Post by xenos »

It would probably make developing easier if you 1. figure out yourself how to implement this in assembly and 2. never use it later, but instead practice the conversion to binary in your head. Actually this is exactly the strategy I would follow, just to practice thinking in terms of registers and bits :) Implementing this really shouldn't be too hard and it's a nice problem to think about.

Fun fact: My watch displays the time in binary.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Binary Function

Post by iansjack »

It would be an interesting, simple excercise for someone taking their first steps in assembler programming. Practical value - zero, as there are already simple ways to do this conversion on any platorm.

This is not a question about OS development.
User avatar
supershirobon
Posts: 5
Joined: Sun Feb 10, 2013 10:03 am
Location: Japan

Re: Binary Function

Post by supershirobon »

I ripped this straight from my project, the concept is easy.
keep dividing by base 2 and add the ascii '0'

Code: Select all

Bin2Ascii:
	;全ての汎用レジスターは破壊されます
	mov cx, 0x00
	mov bx, 0x02	;2進数
.loop1:
	mov dx, 0	;ゴミデータが入っているかも知れないので
	div bx
	add dl, '0'
	push dx
	inc cx
	cmp ax, 0x02
	jge .loop1
	add al, '0'
	mov [si], al
.loop2:
	pop ax
	inc si
	mov [si], al
	loop .loop2
	;inc si                          ;if your string needs to be null terminated then uncomment this
	;mov al, '$'
	;mov [si], al
	ret
己は汝の空虚なる友
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Binary Function

Post by Brendan »

Hi,
feare56 wrote:I had this idea where you would type in a number with the command 'bin' to output the chosen number in binary for example 'bin 57'. How would I implement this with assembly?
The general approach is the same as how you'd do it in almost any other language (e.g. maybe a loop with "digit = (value % 2) + '0'; value /= 2;" in it).

For assembly, you need to optimise yourself. This means doing things like converting the modulo and the division into something much faster like shifts. For example, you might do this:

Code: Select all

    xor eax,eax              ;eax = 0
    shr ebx,1                ;carry flag = value % 2, ebx = value / 2
    adc al,'0'               ;al = 0 + '0' + carry flag
Of course (for binary but not for decimal) it's easier to do the most significant digit first, so that you don't need to reverse the characters later. This might be something like:

Code: Select all

    mov ecx,32               ;ecx = number of digits to do
.nextDigit:
    xor eax,eax              ;eax = 0
    shl ebx,1                ;carry flag = highest bit, ebx = value * 2
    adc al,'0'               ;al = 0 + '0' + carry flag
    call putChar
    loop .nextDigit

    mov al,'b'               ;Do the suffix!
    call putChar
feare56 wrote:Also would this make developing easier later on or would it be just a "fun" command
It'd be virtually useless on its own. If you built a decent calculator (e.g. something that uses "big rational numbers") then the ability to display the results of calculations in binary might be useful.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Binary Function

Post by Love4Boobies »

I disagree with the spoonfeeding. It wasn't a question about base conversion, and it wasn't one about assembly either---presumably the OP understands both? If that were so, he'd have no problem implementing this on his own. No, the "question" was really a subtle request to do it for him. And I suspect he just tricked you into doing his homework for him.

*awaits the denial*
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Binary Function

Post by Combuster »

I'm guessing he's still missing the implementation of atoi()... :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
feare56
Member
Member
Posts: 97
Joined: Sun Dec 23, 2012 5:48 pm

Re: Binary Function

Post by feare56 »

This was not a trick to get free code. I initially thought that somebody would point me in the right direction without the code but thank you
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Binary Function

Post by dozniak »

feare56 wrote:This was not a trick to get free code. I initially thought that somebody would point me in the right direction without the code but thank you
Hint: the decimal value 57 is already stored as binary in computer's memory.
Combuster is right, the only function you really need there is atoi.
Learn to read.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Binary Function

Post by egos »

If TS does not have output function that supports formatting then "to output the chosen number in binary" he should use Brendan's code or something like itoa (with radix=2) if presents.
If you have seen bad English in my words, tell me what's wrong, please.
Post Reply