flat assembler
Message board for the users of flat assembler.

Index > DOS > how to convert byte to string ?

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
it works:
Code:
org 100h

jmp @f
_num db "12345",0 ;i forgot - number has to be ended with 0, not '$'
_w db "works$"

@@:
 mov si,_num
 call str2w
 cmp ax,12345
 jne @f
 mov dx,_w
 mov ah,9
 int 21h
@@:
 retn

str2w:
mov bx,10
xor ax,ax
.digit:
mov cl,[si]
cmp cl,0
je .r ;end if no more digits
mul bx ;if some more digits then multiply result by 10
sub cl,'0' ;convert ascii digit to binary one
xor ch,ch  ;zero-extend it to word
add ax,cx ;add to result
inc si ;and move to next digit
jmp .digit
.r:
retn    

maybe you didn't get what it is inteded to do. Or you didn't notice number should be ended by 0 byte, not '$' Smile

btw:
cmp al, 048
cmp al, 058 / jge
sub al, 048
is better witten as
cmp al,'0'
cmp al,'9' / jg
sub al,'0'
'0' is same as 48, '9' is same as 57 etc.
Post 06 Oct 2005, 20:51
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
vid, you know what i mean.
you dont have to explain type casting to me.
Post 07 Oct 2005, 04:37
View user's profile Send private message Visit poster's website Reply with quote
Bitdog



Joined: 18 Jan 2004
Posts: 97
Bitdog
Here's some upper/lower info for ya to mess with......

Bit 5 is the toggle for Lower case or upper case alph.
If the character is in AL
OR AL,00100000b ;to lower case (32d or 20h = binary shown)
AND AL,11011111b ; to upper case (223d or 0DFh = binary shown)
Post 09 Oct 2005, 09:29
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
xor al,20h ; toggle case

Bitdog wrote:
Here's some upper/lower info for ya to mess with......

Bit 5 is the toggle for Lower case or upper case alph.
If the character is in AL
OR AL,00100000b ;to lower case (32d or 20h = binary shown)
AND AL,11011111b ; to upper case (223d or 0DFh = binary shown)
Post 10 Oct 2005, 07:51
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
matrix: Wink
Post 10 Oct 2005, 12:35
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner
hi, could i know why the mov bx, 10 must be inside loop?
Code:
.loop: 
xor dx,dx 
mov bx,10 
div bx 
add dl,"0" 
mov [si],dl 
dec si 
loop .loop
    

i mean, it could be put before the loop, since nobody would modify the content of bx.
Code:
mov bx,10 
.loop: 
xor dx,dx 
div bx 
add dl,"0" 
mov [si],dl 
dec si 
loop .loop
    
Post 13 Nov 2005, 13:19
View user's profile Send private message Visit poster's website Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
vbVeryBeginner:
true true

Bitdog wrote:
Here's some upper/lower info for ya to mess with......

Bit 5 is the toggle for Lower case or upper case alph.
If the character is in AL
OR AL,00100000b ;to lower case (32d or 20h = binary shown)
AND AL,11011111b ; to upper case (223d or 0DFh = binary shown)


yes, indeed, if you read http://maven.smith.edu/~thiebaut/ArtOfAssembly/artofasm.html
there are some advanced codes with magical values on this forum i remember too, that use "magical values" for this type of conversion,
so sub, add in this case can be left out, logical operations are simpler than addition, substraction.
( however on modern processors they can be even paralleled, and we are moving towards risc architecture, more and more segments of processor working in parallel, so not big difference, but its there. )

anyway there can be a case when you need range checking too, you cant convert "1%%!/=" for example to upper case, and not convert binary digit "z" to ascii digit .
Post 28 Nov 2005, 00:39
View user's profile Send private message Visit poster's website Reply with quote
SDragon



Joined: 13 Sep 2005
Posts: 19
Location: Siberia
SDragon
Code:
toupper: ; input: [ecx] = string
@@: ; uses eax, edx
  movzx edx, byte[ecx]
  lea eax, [edx - 'a']
  sub eax, 26
  sbb eax, eax  ; all zeros if eax < 26, all ones otherwise
  and eax, 'a' - 'A'
  sub byte[ecx], al
  add ecx, 1
  test edx, edx
  jne @B
  ret

tolower: ; input: [ecx] = string
@@: ; uses eax, edx
  movzx edx, byte[ecx]
  lea eax, [edx - 'A']
  sub eax, 26
  sbb eax, eax  ; all zeros if eax < 26, all ones otherwise
  and eax, 'a' - 'A'
  add byte[ecx], al
  add ecx, 1
  test edx, edx
  jne @B
  ret
    


Sorry for 32-bit code. Hope you can adapt this for DOS Wink.
Post 29 Nov 2005, 12:08
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.