1.11M Members

explain code that involves characters

 
0
 

Hi people can someone explain this code to me clearly, this is the simplest code I could possibly get I just want to ask how it works hope you can help me. By the way this code asks for an uppercase letter and will convert it to its lowercase equivalent. Can someone translate to me what exactly each lines of code does?? Well some lines already have comments but still I cannot relate it to the other lines without comments so it's quite confusing...please please.. I am new to assembly programming please help me, thank you!

code segment
    assume cs:code, ds:code
    org 100h
start:
    mov ah,09h  ;display string
    mov dx, offset msg1
    int 21h

    mov ah,01h  ;ask user
    int 21h
    add al,20h  ;converts uppercase to lowercase
    mov bl,al

    mov ah,02
    mov dl,0ah
    int 21h

    mov ah,02
    mov dl,0dh
    int 21h

    mov ah,09h  ;display string
    mov dx, offset msg2
    int 21h

    mov dl,bl
;   mov dl,al   ;lowercase equivalent
    mov ah,02   ;display the value of dl
    int 21h
    int 20h

    msg1:db 'enter uppercase letter: $'
    msg2:db 'lowercase equivalent: $'
code ends
end start
 
0
 

Let's start with int 21h, because it seems like magic if you're not familiar with the multipurpose functionality. It behaves differently depending on the value of the AX register.

mov is just shuffling data around, I'm not sure I need to explain it unless you have a specific question.

Line 11 is where the real work of the conversion is done:

add al,20h  ;converts uppercase to lowercase

It's brittle in that the program doesn't check to make sure the character has a lower case equivalent before doing the conversion, but in the ASCII character set upper and lower case letter blocks are 32 characters apart, so if you add 32 (which is 20h in hexadecimal) to an upper case character you'll get the corresponding lower case character, and if you subtract 32 from that, you'll get the original upper case character.

For example, 'A' has a value of 65 and 'a' a value of 97. 65 + 32 = 97 and 97 - 32 = 65. Easy peasy.

int 20h terminates the program.

 
0
 

ah and al are both under the ax registry right? well is there a significant difference between ah an al? I mean is the purpose of ah same as al like for example, in outputing a string we use:

mov ah, 09

instead of using that can I use:

mov al, 09

??? Will there be no difference between them? And can I also use al to all other movs that has ah in it? Will there be no problems with that?

 
0
 

ah and al are both under the ax registry right? well is there a significant difference between ah an al?

AH and AL are the upper and lower bytes of the AX register. Much like AX is the lower 16 bits of EAX and EAX is the lower 32 bits of RAX. They're general registers, but some instructions expect operands in a specific register or part of a register or write to a specific register or part of a register.

For example, let's take this block:

mov ah,01h  ;ask user
int 21h
add al,20h  ;converts uppercase to lowercase
mov bl,al

int 21h (if you read the documentation I linked earlier) states that an operand in AH of 01h will cause a character to be read from standard input, and the character that's read will be stored in AL. BL is the more permanent storage for the character as other work is performed using AX prior to printing that character.

And before you ask, yes, it could also be done like this:

mov ah, 01h
int 21h
mov bl, al
add bl, 20h

There's not really any reason to add before moving or move before adding. There may be a performance reason I'm not familiar with, but in this case I doubt it given that you're working with two general registers of the same size.

 
0
 

I'm getting the concept now, I'm still learning though. Its like it was transferred to another memory which is bl because bl is capable of handling more and much intense instructions, am I right?

 
1
 

because bl is capable of handling more and much intense instructions, am I right?

Not really. AL and BL are functionally identical, but because AL will be reused almost immediately by the next few instructions, its contents must be saved into another register or they'll be lost.

 
0
 

very clear! thanks!

 
0
 

how about the ah and dl, specifically the code below. What exactly was the purpose of ah and dl in the code below?

 mov ah,02
    mov dl,0ah
    int 21h
    mov ah,02
    mov dl,0dh
    int 21h
 
0
 

You clearly didn't read the reference links I so kindly gave you earlier. Here it is again. AH tells the interrupt what to do, and DL is the argument.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: