lab 5 part c write to the screen a character string that uses a ‘$’ to indicate the end of the...

25
Lab 5 Part C • Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt 21h Function 2 to write each character of the string to the screen.

Upload: coral-hopkins

Post on 27-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Lab 5 Part C

• Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt 21h Function 2 to write each character of the string to the screen.

Page 2: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

How to Compare

• Want to compare each character in the string to ‘$’ character. – If not equal, write character– Else, exit program

• Alternatively,– If equal, exit program– Else, write character

Page 3: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Moving Through a String of Characters

Use a pointer register to point to the beginning of the string. (SI)

Loop through the string one character at a time. (INC SI)

Page 4: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Writing a String to the Screen

Move value pointed to into register DL

Is Equal to ‘$’’

Write character on screen

NO

Exit Program YES

S

Increment pointer register SI

Move beginning of string into pointer register SI

Page 5: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Instructions for Writing to ScreenExiting Program

Writing to the screen using Interrupt 21h

(the character to be written must be stored in DL)

Mov ah, 2 ;put function number in ah

Int 21h ;call interrupt 21h

Exiting program

Mov ah, 4Ch ;put function number in ah

Int 21h ;call interrupt 21h

Page 6: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Compare Loop

MOV SI, OFFSET string1

LP1: MOV DL, [SI] ;Note DL

CMP DL, ‘$’

JE Exit

……. ;print here

INC SI

JMP LP1

Page 7: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Put code inside template.codeMain proc

mov ax, @data ;initialize data segmentmov ds, axmov SI, offset string1 ;SI is pointer to string1

LP1: mov dl, [SI] ;save char in dlcmp dl, ‘$’ ;compare char to ‘$’je exitinc SI ;point to next charmov ah, 2 ;print charint 21hjmp LP1 ;end of loop

EXIT: mov ah, 4Ch ;exit programint 21h

Main endpEND Main

Page 8: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Writing a Character String in UpperCase, LowerCase or Reversing Case

• OR each Character with 20h creates uppercase characters

• AND each character with DFh creates Lowercase characters

• XOR each character with 20h reverses the case. Uppercase becomes lowercase; Lowercase becomes uppercase

Page 9: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Lab 5 Part D

• Write to the screen an 8-bit number that is stored in memory. Write the number in binary. Write each binary bit as a character using DOS Interrupt 21h, function 2.

Page 10: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Dealing with Bits

• To look at each bit, we need to use instructions that deal with bits. The shift and rotate instructions allow us to evaluate individual bits.

• shr bl, 1

the lsb is placed in the carry flag

• shl bl, 1

The msb is placed in the carry flag

Page 11: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Jumps Based on Status

• A conditional jump based on the value of the carry status flag would then be taken

Page 12: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

What is a 1 and 0 in ASCII

0 = 30h

1 = 31h

Therefore, move dl, 30h if carry flag = 0

Move dl, 31h if carry flag = 1

Page 13: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Pseudocode

Move number into register from memorySetup loop = 8 to access each bit individuallyLP1: Shift each bit into carry flag

Jump to Is_One procedure if carry = 1Else move into DL, 30hJump to print label

Is_One: Move into DL, 31hPrint: Use DOS interrupt Function 2 to write char

to screen.Loop back to LP1 if CX >= 0

Exit

Page 14: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Writing a Number in Binary

Shift BL by one to get msb into carry flag

Carry Equals 1?

Move 30h into DL

NO

Mov 31h into DLYES

S

Write character on screen

Move 8 into CX (dealing with a byte value)

Move Number into BL

Page 15: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Lab 6 Part A

• Write a number in hexadecimal. Use DOS interrupt 21h, function 2 to write a character to the screen.

• Write a CRLF procedure since this function is used so often. Use the DOS interrupt 21h, function 2 to write the appropriate characters to generate a CR and LF on the screen.

Page 16: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Converting a Decimal Digit to a Hex Digit

• You know how to change a bit that represents a decimal value 0-1 to an ASCII character value. Add 30h to the value.

• Use 4 bits to represent the decimal values 0-9.

• Compare the value to see if the value is greater than 9. If so, it must be a character A-F.

Page 17: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

How do You only look at 4 bits out of a Byte Size Register?

• MASK the upper four bits of the byte. Be sure to save the value of the byte before you mask it; the register will get changed.

• Use AND 0Fh to keep the lower four bits; hide the upper four bits of the 8 bit register value.

• You now have a value between 0 and 15.• Compare to 9

– Equal or less – no change (add 30h to get ASCII value)

– Greater => must be a character. (add 37h to get ASCII value)

Page 18: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Convert_to_Hex Procedure (Changes the value of AL)

Convert_to_Hex procAnd al, 0FhCmp al, 9

Ja ischarAdd al, 30hjmp exit

Ischar: Add al, 37hExit: retConvert_to_Hex proc

Page 19: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

How do you write a Carriage Return to the screen

• There is an ASCII character labelled “Carriage Return” (0Dh)

• There is an ASCII character labelled “Line Feed” (0Ah)

Page 20: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

PCRLF Procedure

Pcrlf Proc

mov ah, 2

mov dl, 0Ah

int 21h

mov dl, 0Dh

int 21h

ret

Page 21: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Print_char Procedure

Print_char proc

mov ah, 2

int 21h

ret

Print_char endp

Page 22: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Main ProgramMain proc

mov ax, @datamov ds, axpushaxcall convert_to_hexmov dl, alcall print_charcall pcrlfmov ax, 4c00hint 21h

Main endp

Page 23: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Order of Procedures

Main proc…..

Main endpConvert_to_Hex proc

…..Convert_to_Hex endpPrint_char proc

…..Print_char endpPcrlf proc

…..Pcrlf endpEnd Main

Page 24: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

ASCII Decimal String to Hex Conversion

• Given a two digit unsigned decimal string, with each digit in ASCII, convert it to the correct 8-bit value.

Page 25: Lab 5 Part C Write to the screen a character string that uses a ‘$’ to indicate the end of the string. Do not write the ‘$’ to the screen. Use DOS Interrupt

Conversion from ASCII

• The first ASCII value is the 10’s digit. Subtract 30h from the value to get the unsigned binary (or hex) representation, then multiply it by 10.

• Subtract 30h from the second digit, and then add it to the result of the first operation.