Page 1 of 1
Binary Function
Posted: Sun Feb 17, 2013 10:47 pm
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
Re: Binary Function
Posted: Mon Feb 18, 2013 12:22 am
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.
Re: Binary Function
Posted: Mon Feb 18, 2013 1:28 am
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.
Re: Binary Function
Posted: Mon Feb 18, 2013 1:39 am
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
Re: Binary Function
Posted: Mon Feb 18, 2013 3:03 am
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
Re: Binary Function
Posted: Mon Feb 18, 2013 4:58 am
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*
Re: Binary Function
Posted: Mon Feb 18, 2013 5:11 am
by Combuster
I'm guessing he's still missing the implementation of atoi()...
Re: Binary Function
Posted: Mon Feb 18, 2013 10:29 am
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
Re: Binary Function
Posted: Tue Feb 19, 2013 2:06 am
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.
Re: Binary Function
Posted: Tue Feb 19, 2013 4:08 am
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.