CSC167 ยท 2081 ยท 5 marks
Write an ALP to convert the string 'computer science' to upper case using 16 bit microprocessor.
Write an ALP to convert the string 'computer science' to upper case using 16 bit microprocessor. [5]
MicroprocessorChapter 4: Assembly Language ProgrammingSimple sequence programs
Quick answer
In ASCII, lowercase letters range from 61H ('a') to 7AH ('z') and uppercase letters range from 41H ('A') to 5AH ('Z'). To convert lowercase to uppercase, we subtract 20H from the ASCII value of each lowercase character.
ALP to Convert String 'computer science' to Upper Case (16-bit Microprocessor / 8086)
Concept
Spaces and other non-alphabetic characters are left unchanged.
Assembly Language Program
.MODEL SMALL
.STACK 100H
.DATA
STRING DB 'computer science', '$' ; Define the string with $ terminator
LEN DW 16 ; Length of the string (16 characters)
.CODE
MAIN PROC
MOV AX, @DATA ; Load address of DATA segment into AX
MOV DS, AX ; Initialize Data Segment register
MOV SI, OFFSET STRING ; SI points to the start of the string
MOV CX, LEN ; CX = loop counter = length of string
BACK:
MOV AL, [SI] ; Load current character into AL
CMP AL, 'a' ; Compare AL with 'a' (61H)
JB SKIP ; If AL < 'a', not a lowercase letter, skip
CMP AL, 'z' ; Compare AL with 'z' (7AH)
JA SKIP ; If AL > 'z', not a lowercase letter, skip
SUB AL, 20H ; Convert lowercase to uppercase by subtracting 20H
MOV [SI], AL ; Store the converted character back into memory
SKIP:
INC SI ; Move SI to point to next character
LOOP BACK ; Decrement CX and repeat if CX != 0
; Display the converted string
MOV DX, OFFSET STRING ; Load address of converted string into DX
MOV AH, 09H ; DOS function 09H: display string
INT 21H ; Call DOS interrupt
MOV AX, 4C00H ; DOS function 4CH: terminate program
INT 21H
MAIN ENDP
END MAIN
Step-by-Step Explanation
| Step | Instruction | Purpose |
|---|---|---|
| 1 | MOV AX, @DATA / MOV DS, AX | Initialize the Data Segment |
| 2 | MOV SI, OFFSET STRING | SI points to first character of string |
| 3 | MOV CX, LEN | Set loop counter to string length (16) |
| 4 | MOV AL, [SI] | Load each character one by one |
| 5 | CMP AL, 'a' and CMP AL, 'z' | Check if character is lowercase |
| 6 | SUB AL, 20H | Convert to uppercase (e.g., 'c' = 63H - 20H = 43H = 'C') |
| 7 | MOV [SI], AL | Write converted character back |
| 8 | INC SI / LOOP BACK | Advance to next character and repeat |
| 9 | INT 21H (AH=09H) | Display the final uppercase string |
Example Conversion
| Character | ASCII (Hex) | After SUB 20H | Result |
|---|---|---|---|
| 'c' | 63H | 43H | 'C' |
| 'o' | 6FH | 4FH | 'O' |
| ' ' (space) | 20H | Skipped | ' ' |
| 's' | 73H | 53H | 'S' |
Output: COMPUTER SCIENCE
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.