CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

19
CEN 226 : Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman

Transcript of CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

Page 1: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

CEN 226: Computer Organization & Assembly Language :CSC 225(Lec#6)

ByDr. Syed Noman

Page 2: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

2

Subprograms/Procedures•A subprogram allows us to give a name to a

group of instructions and to use that name when we wish to execute those instructions, instead of having to write the instructions again.

•For example, the instructions to display a character could be given the name putc (or whatever you choose). Then to display a character you can use the name putc which will cause the appropriate instructions to be executed.

Page 3: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

3

Call Instruction

•This is referred to as calling the subprogram. In 8086 assembly language, the instruction call is used to invoke a subprogram, so for example, a putc subprogram would be called as follows:▫Call putc ; Display character in dl

•The process of giving a group of instructions a name is referred to as defining a subprogram. This is only done once.

Page 4: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

4

Ret instruction

•The ret instruction terminates the subprogram and arranges for execution to resume at the instruction following the call instruction.

Page 5: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

5

Procedures for character and string outputputs: ; display a string terminated by $ ; ax contains address of stringmov dx, ax ; copy address to dx for ms-dosmov ah, 9hint 21h ; call ms-dos to output stringret

putc: ; display character in almov dl, al ; copy al to dl for ms-dos mov ah, 2hint 21hret

Page 6: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

6

Procedure for character input

getc: ;read character into almov ah, 1hint 21hret

Page 7: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

7

Important point about sub-programs

• We usually refer to that part of a program where execution begins as the main program .

• In practice, programs consist of a main program and a number of subprograms. It is important to note that subprograms make our programs easier to read, write and maintain even if we only use them once in a program.

• Note : Subprograms are defined after the code to terminate the program, but before the end directive.

• If we placed the subprograms earlier in the code, they would be executed without being called (execution would fall through into them). This should not be allowed to happen.

Page 8: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

8

Printing string using procedure• The following program, displays the message ‘Hello World’, and uses the equ directive.

.model small

.stack 100h

.dataCR equ 13dLF equ 10dmessage db ‘Hello World’, CR, LF, ‘$’.codestart:mov ax, @datamov ds, axmov dx, offset messagecall puts ; display messagemov ax, 4c00hint 21h; User defined subprogramsputs: ; display a string terminated by $ ; dx contains address of stringmov ah, 9hint 21h ; output stringretend start

Page 9: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

9

Symbolic constants

•A symbolic constant is created by associating an identifier with an integer expression or some text. Symbols do not reserve storage. They are used only by the assembler when scanning a program, and they cannot change at run time.

•The EQU directive associates a symbolic name with an integer expression or some arbitrary text.

Page 10: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

10

Character Conversion: Uppercase to Lowercase & vice-versa

• To convert an uppercase letter to lowercase, we note that ASCII codes for the uppercase letters ‘A’ to ‘Z’ form a sequence from 65 to 90.

• The corresponding lowercase letters ‘a‘ to ‘z’ have codes in sequence from 97 to 122.

• We say that ASCII codes form a collating sequence and we use this fact to sort textual information into alphabetical order.

• To convert from an uppercase character to its lowercase equivalent, we add 32 to the ASCII code of the uppercase letter to obtain the ASCII code of the lowercase equivalent.

• To convert from lowercase to uppercase, we subtract 32 from the ASCII code of the lowercase letter to obtain the ASCII code of the corresponding uppercase letter.

• The number 32 is obtained by subtracting the ASCII code for ‘A’ from the ASCII code for ‘a’

i.e. (‘A’ - ‘a’ = 97 - 65 = 32).

Page 11: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

11

Character conversion program with procedures

1. ; char4.asm: character conversion: ;upper to lowercase

2. .model small3. .stack 100h4. CR equ 13d5. LF equ 10d6. .data7. reply db ‘y’8. msg0 db CR, LF, ‘Enter y to

continue: $’9. msg1 db CR, LF, ‘Enter an

uppercase letter: $’10. result db CR, LF, ‘The lowercase

equivalent is: $’11. .code12. ; main program13. start:14. mov ax, @data15. mov ds, ax16. readloop:17. cmp reply, ‘y’ ; while (reply == ‘y‘)

18. jne finish ; do loop body19. mov ax, offset msg120. call puts ; prompt for letter21. call getc ; read character22. mov bl, al ; save character in bl23. add bl, 32d ; convert to

lowercase24. mov ax, offset result25. call puts ; display result

message26. mov al, bl27. call putc ; display lowercase

letter28. mov ax, offset msg029. call puts ; prompt to continue30. call getc ; read reply31. mov reply, al ; save character in

reply32. jmp readloop ; repeat loop test33. finish:34. mov ax, 4c00h35. int 21h ; return to ms-dos36. ; user defined subprograms

should be ;defined here37. end start

Page 12: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

12

Program (char4.asm) output• Executing this program produces as output:

Enter an uppercase letter: G The lowercase equivalent is: g

• The string result is defined to begin with the “Return” and “Line-feed” characters so that it will be displayed on a new line. An alternative would have been to include the two characters at the end of the string msg1, before the ‘$’ character, e.g. ▫msg1 db ‘Enter an uppercase letter: ’,CR, LF, ‘$’

• After displaying msg1, as defined above, the next item to be displayed will appear on a new line.

Page 13: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

13

Assignment 2b

•Write a program that takes a character from the keyboard and converts it to the opposite case (lowercase to uppercase or vice versa) if it is an English alphabet; otherwise the program prints the message “The character is not an English alphabet!”

Page 14: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

14

Guessing Game Program

•Write a code fragment to read a character entered by the user and compare it to the character ‘A’. Display an appropriate message if the user enters an ‘A’. This code fragment is the basis of a guessing game program.

Page 15: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

15

Guessing Game Program: C code fragment

•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 “) ;

Page 16: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

16

Guessing Game Program : Assembly code fragmentmov ax, offset prompt ; prompt usercall putscall getc ; read charactercmp al, ‘A’ ; compare it to ‘A’jne is_not_an_a ; jump if not ‘A’mov ax, offset yes_msg ; if actioncall puts ; display correct guessjmp end_else ; skip else actionis_not_an_A: ; else actionmov ax, offset no_msgcall puts ; display wrong guessend_else:

Page 17: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

17

Guessing Game Program (complete).model small.stack 100hCR equ 13dLF equ 10d.dataprompt 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 $”.codestart:mov ax, @datamov ds, axmov ax, offset promptcall puts ; prompt for inputcall getc ; read charactercmp al, ‘A’jne is_not_an_a ; if (al != ‘A’) skip action

mov ax, offset yes_msg ; if actioncall puts ; display correct guessjmp end_else1 ; skip else actionis_not_an_A: ; else actionmov ax, offset no_msgcall puts ; display wrong guessend_else1:finish: mov ax, 4c00hint 21h; User defined subprograms; < puts getc defined here>end start

Page 18: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

18

Assignment 2c

•Write an assembly language program that prints the previous and the forward character for a digit entered within the range of 0-9. use messages for -1 and 10.

Page 19: CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6) By Dr. Syed Noman.

19

Assignment 2d

•Write an assembly language program that input two digits within the range of 0 to 9 and print the sum if the sum is within the range of 1 to 9, otherwise prints the message “Out of range!”. The program should ask the user for the repetition of the same process.