lab 5 part c write to the screen a character string that uses a ‘$’ to indicate the end of the...
TRANSCRIPT
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.
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
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)
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
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
Compare Loop
MOV SI, OFFSET string1
LP1: MOV DL, [SI] ;Note DL
CMP DL, ‘$’
JE Exit
……. ;print here
INC SI
JMP LP1
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
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
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.
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
Jumps Based on Status
• A conditional jump based on the value of the carry status flag would then be taken
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
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
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
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.
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.
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)
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
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)
PCRLF Procedure
Pcrlf Proc
mov ah, 2
mov dl, 0Ah
int 21h
mov dl, 0Dh
int 21h
ret
Print_char Procedure
Print_char proc
mov ah, 2
int 21h
ret
Print_char endp
Main ProgramMain proc
mov ax, @datamov ds, axpushaxcall convert_to_hexmov dl, alcall print_charcall pcrlfmov ax, 4c00hint 21h
Main endp
Order of Procedures
Main proc…..
Main endpConvert_to_Hex proc
…..Convert_to_Hex endpPrint_char proc
…..Print_char endpPcrlf proc
…..Pcrlf endpEnd Main
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.
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.