Binary Function
Binary Function
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
- xenos
- Member
- Posts: 1121
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- Contact:
Re: Binary Function
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.
Fun fact: My watch displays the time in binary.
Re: Binary Function
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.
This is not a question about OS development.
- supershirobon
- Posts: 5
- Joined: Sun Feb 10, 2013 10:03 am
- Location: Japan
Re: Binary Function
I ripped this straight from my project, the concept is easy.
keep dividing by base 2 and add the ascii '0'
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
Hi,
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:
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:
Cheers,
Brendan
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).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?
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
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
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.feare56 wrote:Also would this make developing easier later on or would it be just a "fun" command
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.
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Binary Function
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*
*awaits the denial*
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
- Combuster
- 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
I'm guessing he's still missing the implementation of atoi()...
Re: Binary Function
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
Hint: the decimal value 57 is already stored as binary in computer's memory.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
Combuster is right, the only function you really need there is atoi.
Learn to read.
Re: Binary Function
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.