Character Conversion: Uppercase To Lowercase: Introduction To 8086 Assembly Language Programming (Alp4)
Character Conversion: Uppercase To Lowercase: Introduction To 8086 Assembly Language Programming (Alp4)
.model small
.stack 100h
CR equ 13d
LF equ 10d
.data
msg1 db ‘Enter an uppercase letter: $’
result db CR, LF, ‘The lowercase equivalent is:
$’
.code
; main program
start:
mov ax, @data
mov ds, ax
end start
.model small
.stack 100h
CR equ 13d
LF equ 10d
.data
.code
; main program
start:
mov ax, @data
mov ds, ax
end start
This can cause subtle but serious errors, in programs, that are
difficult to detect. The following code fragment illustrates the
problem:
mov dx, 12 ; dx = 12
(In the case of getc, however, we would not save the value
of the al register because we want getc to read a value into
that register.)
To get the item back from the stack, we “pop the item from
the stack”.
The 8086 provides push and pop instructions for storing and
retrieving items from the stack. See Chapter 2 for details.
mov dx, ax
mov ah, 9h
int 21h ; call ms-dos to output string
pop dx ; restore dx
pop cx ; restore cx
pop bx ; restore bx
pop ax ; restore ax
ret
mov dl, al
mov ah, 2h
int 21h
pop dx ; restore dx
pop cx ; restore cx
pop bx ; restore bx
pop ax ; restore ax
ret
mov ah, 1h
int 21h
pop dx ; restore dx
pop cx ; restore cx
pop bx ; restore bx
ret
Note that we pop values from the stack in the reverse order
to the way we pushed them on, due to the last-in-first-out
(LIFO) nature of stack operations.
again:
call getc ; read a character
call putc ; display character
jmp again ; jump to again
You may place a label at any point in your program and the
label can be on the same line as an instruction e.g.
finish:
mov ax, 4c00h
int 21h
In this case the code between jmp instruction and the label
finish will not be executed because the jmp causes control
to skip over it.
If the Z-flag has value 1, it means that the result of the last
instruction which affected the Z-flag was 0.
If the Z-flag has value 0, it means that the result of the last
instruction which affected the Z-flag was not 0.
mov ax, 2 ; ax = 2
sub ax, bx ; ax = 2 - bx
jz nextl ; jump if (ax-bx) == 0
inc ax ; ax = ax + 1
nextl:
inc bx
ax = 2;
if ( ax != bx )
{
ax = ax + 1 ;
}
bx = bx + 1 ;
cmp ax, bx
je equals ; jump if ax == bx
This name for the instruction makes the code more readable in
a situation where we are testing two values for equality.
jo overflow of = 1
jno not overflow of = 0
js sign sf = 1
jns no sign sf = 0
The letter g can be taken to mean greater than and the letter l
to mean less than. Instructions using these letters (e.g. jg, jl
etc.) operate on signed numbers.
if (condition )
{
/* action statements */
}
<rest of program>
Example 3.27:
C version
if ( i == 10 )
{
i = i + 5 ;
j = j + 5 ;
}
/* Rest of program */
8086 version 1:
cmp i, 10
je label1 ; if i == 10 goto label1
jmp rest ; otherwise goto rest
label1: add i, 5
add j, 5
rest: ; rest of program
8086 version 2:
cmp i, 10
jne rest ; if i != 10 goto rest
add i, 5 ; otherwise do action part
add j, 5
rest: ; rest of program
if ( condition )
{
/* action1 statements */
}
else
{
/* action2 statements */
}
C version:
printf(“Guessing game: Enter a letter (A
to Z): “);
c = getchar() ;
if ( c == ‘A’ )
printf(“You guessed correctly !! “);
else
printf(“Sorry incorrect guess “) ;
end_else:
If the value read is the letter ‘A‘, then the jne will not be
executed, yes_msg will be displayed and control transferred
to end_else. If the value entered is not an ‘A‘, then the
jne is executed and control is transferred to is_not_an_A.
.model small
.stack 100h
CR equ 13d
LF equ 10d
.data
prompt db “Guessing game: Enter a letter (A to Z):
$“
yes_msg db CR, LF,“You guessed correctly !! $“
no_msg db CR, LF,“Sorry incorrect guess $“
.code
start:
mov ax, @data
mov ds, ax
mov ax, offset prompt
call puts ; prompt for input
end_else1:
end start
8086 version:
.model small
.stack 100h
CR equ 13d
LF equ 10d
.data
invalid:
mov ax, offset bad_msg ; not uppercase
call puts ; display bad_msg
mov al, bl
call putc ; display character
entered
finish:
mov ax, 4c00h
int 21h ; return to ms-dos
end start
C version:
count = 1 ;
while ( count <= 60 )
{
putchar(‘*’) ;
count = count + 1 ;
}
8086 version:
mov cx, 1d ; cx = 1
mov al, ‘*’ ; al = ‘*’
disp_char:
cmp cx, 60d
jnle end_disp ; if cx > 60 goto end_disp
call putc ; display ‘*’
inc cx ; cx = cx + 1
jmp disp_char ; repeat loop
test
end_disp:
C version:
c = ‘a‘ ; /* c = 97 (ASCII for ‘a‘)
while ( c <= ‘z‘ )
{
putchar( c );
c = c + 1 ;
}
8086 version:
mov al, ‘a’
startloop:
cmp al, ‘z’
jnle endloop ; while al <= ‘z’
call putc ; display character
inc al ; al = al + 1
jmp startloop ; repeat test
endloop:
C version:
main()
{
char c, reply;
reply = ‘y‘;
finish:
mov ax, 4c00h
int 21h ; return to ms-dos
; user defined subprograms should be defined here
end start
Exercises
3.16 Modify the program in Example 3.33 to test that the
letter entered is a valid uppercase letter. If it isn’t an
uppercase letter a suitable error message should be displayed
and the program should continue executing for as long as the
user wishes.
disp_char:
Why ?