flat assembler
Message board for the users of flat assembler.

Index > DOS > toupper

Author
Thread Post new topic Reply to topic
denial



Joined: 12 Sep 2004
Posts: 98
denial
Hello,

just for learning, I tried to write the toupper function from C in assembly. The function takes a string and converts non-capitalized letters into capitalized letters. The program I wrote works fine, but I wanted to know how to improve it, and what could I make better next time. Here's what I got.
As you can see, the code will produce the output
"before: [string as it was defined]
after: [string after using toupper]"

Code:
org 100h

_main:
        mov ah,9
        mov dx,_before
        int 21h
        mov dx,_string
        int 21h
        mov dx,_linebreak
        int 21h
        mov dx,_after
        int 21h
        mov bx,_string
        call _toupper
        int 20h

_toupper:
        mov ah,9
        @@:
        cmp [bx],byte '$'
        je @f
                cmp [bx],byte 97
                jb _toupper_continue
                cmp [bx],byte 122
                ja _toupper_continue
                        sub [bx],byte 32
                _toupper_continue:
                inc bx
                jmp @b
        @@:
        mov dx,_string
        int 21h
        ret

_string db 'heLlO World 8]$'
_before db 'before: $'
_after db 'after: $'
_linebreak db 13,10,'$'
    


Oh and another question: I try to write code that converts a byte into a string, so for example the numerical value 153 should get "153".
I have an idea about how to translate the single digits to the right ascii codes. But how can I access the single digits from the byte? I have to process 1, 5 and 3 individualy. Can I do it with some shifting?

Thank you in advance.
Post 01 Sep 2005, 15:38
View user's profile Send private message Reply with quote
shaolin007



Joined: 03 Sep 2004
Posts: 65
shaolin007
I did this one in NASM but you could easily do it in FASM with a little bit of modification. It asks for input then converts it then displays the modified string.



Code:
                        ;NASM-IDE ASM Assistant Assembler Project File
BITS 16                 ;Set code generation to 16 bit mode
ORG 0x0100              ;Set code start address to 0100h

;TO UPPERCASE USING DOS SERVICES, SIMPLER VERSION!



SEGMENT .text           ;Main code segment

mov ax, 0003h
int 10h

mov ah, 9
mov dx, msg
int 21h

mov ah, 0ah
mov dx, buffer
int 21h

mov si, buffer+2
cld
call To_Uppercase

mov ah, 9
mov dx, crlf
int 21h

mov ah, 9
mov dx, toupper
int 21h

mov ah, 9
mov dx, buffer+2
int 21h

mov ah, 9
mov dx, crlf
int 21h

mov ah, 9
mov dx, anykey
int 21h

mov ah, 0
int 16h

mov ax, 4c00h
int 21h

To_Uppercase:
    cmp byte [si], '$'
    je Done
    lodsb
    and al, 11011111b
    mov [ds:si-1], al
    jmp To_Uppercase

    Done:
    ret

SEGMENT .data           ;Initialised data segment

msg      db "Please enter up to 16 characters to convert to uppercase: ",'$'
buffer   db 17
         db 0
times 17 db 0
         db '$'
         
crlf     db 10,13,10,13,'$'

toupper  db "Conversion to uppercase: ",'$'
anykey   db "Press any key to continue",'$'

SEGMENT .bss            ;Uninitialised data segment
    

[/code]
Post 01 Sep 2005, 20:16
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
Denial,
i think you have done a good work translating toupper and it should meet all your needs in converting, for code size and for speed too.
it should be optimal performance.

as for the byte to string conversion, see some examples (working codes) on how to write byte value word value / dword value on to screen.
basically it is tha same apart from that text mode screen needs 1 byte for color and 1 byte for character value.
example for rendering byte value into string you make divisions by 10 ( for base 10 conversion ) and write mod. until byte value is 0.
Post 03 Sep 2005, 19:48
View user's profile Send private message Visit poster's website Reply with quote
denial



Joined: 12 Sep 2004
Posts: 98
denial
Thank you very much... I'll look at some examples and give it a try.
Post 04 Sep 2005, 10:15
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
i' ve split this to so easier to find, and has own thread:
how to convert byte to string
http://board.flatassembler.net/topic.php?t=4083
Post 04 Sep 2005, 18:01
View user's profile Send private message Visit poster's website 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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.