flat assembler
Message board for the users of flat assembler.

Index > Main > Byte to Integer

Author
Thread Post new topic Reply to topic
Patrick_



Joined: 11 Mar 2006
Posts: 53
Location: 127.0.0.1
Patrick_ 16 Mar 2006, 19:08
Hello all. I wrote myself a hard-drive temperature/model reader in C, and since I'm learning assembly, I decided to re-write it in assembly.

All has gone well, until now. I need to print a retrieved byte as an integer (like printf("value == %d\n", byte);

The retrieved byte is 0x1e, which, converted to an integer, and according to printf(), is 30. I'm reading the data into a buffer in the stack (though I can probably just put it in a register... it's one byte). So, without depending on libc's printf(), how can I print out, or convert, the byte to it's integer equivalent?
Post 16 Mar 2006, 19:08
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 16 Mar 2006, 19:59
That would depend on the operating system you are on.

First, to convert the value to a string equivalent of it, use this code:

Code:
IntToStr:
; eax = number, ebx = base, edi = buffer
push    ecx edx
xor ecx,ecx
  .new:
xor edx,edx
div ebx
push    edx
inc ecx
test    eax,eax
jnz .new 
 .loop:
pop eax
add al,30h 
cmp al,'9'
jng .ok 
add al,7  
 .ok:
stosb
loop    .loop
mov al,0 
stosb
pop edx ecx
ret    

This isnt mine, I found it on this board somewhere, sorry to whoever wrote it for not putting your name here, I just never noted it down. But great code.

You would then output it using the OS-specific API.

For linux, you would use something along the lines of:
Code:
mov eax, 4
mov ebx, 1
mov ecx, addressofstring
mov edx, lengthofstring
int 0x80    


Oh, and a tip, you can find the length of the string made by the code at the top by doing:
Code:
sub edi, addressofstring    

after calling it.

Good luck,
gunblade
Post 16 Mar 2006, 19:59
View user's profile Send private message Reply with quote
Patrick_



Joined: 11 Mar 2006
Posts: 53
Location: 127.0.0.1
Patrick_ 16 Mar 2006, 20:21
Thanks for the response. However, I have a few questions:

First, since it's my third day of learning/programming in assembly, could you please comment the lines? I can understand some of it, but I'm not quite sure what's totally going on.

Also, the comment says that ebx is the base... what do you mean by base? Like base-10, base-6 (hex), etc? Or something else?

Thanks for that tip, too. I was wondering how I'd do that. Smile
Post 16 Mar 2006, 20:21
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 16 Mar 2006, 22:38
Check here for a fast bin2dec routine (Terje Mathisen, GPL): http://board.flatassembler.net/topic.php?t=3924
Post 16 Mar 2006, 22:38
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 17 Mar 2006, 08:08
Yeah, it should be the fastest (60 clocks) against the traditional divide_by_10_and_take_the_reminder approach Razz where every such division takes at least 49 clocks and you need upto ten of these.
Post 17 Mar 2006, 08:08
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.