# CSE 322 Lab 5

March 6, 2018

#### Problem#1 (Section 6.5)

Write a code to take a string input and print the first and last capital letters in the string in alphabetical order

//Solution

```TITLE First and last capitals
.MODEL SMALL ; DIRECTIVE
.STACK 100H
.DATA ;
CR EQU 0DH ;
LF EQU 0AH ;
;A DW 2 ; DATA SEGMENT
;B DW 5 ;
;SUM DW ? ;
msg db 'Type a line of text',cr,lf,'\$'
msg_nocap db cr,lf,'There is no capital\$'
cap_msg db cr,lf, 'First Capital = '
first db ']'
db ' Last Capital = '
last db '@\$'

.CODE ;
MAIN PROC ;MAIN PROCEDURE ; CODE SEGMENT
MOV AX,@DATA ;
MOV DS, AX ;

mov ah, 9
lea dx, msg
int 21h

MOV AH, 1
INT 21h
while:
cmp al, cr
je end_while

;STaring of if statement
;if
cmp al,'A'
jnge end_if
cmp al, 'Z'
jnle end_if
;then
cmp al, first
jnl check_last
mov first, al

check_last:
cmp al, last
jng end_if
mov last,al

end_if:

int 21h
jmp while
end_while:

mov ah, 9

cmp first,']'
jne cap

lea dx, msg_nocap
jmp display

cap:
lea dx, cap_msg

display:

int 21h

;Exit code
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
```

#### Problem#2

Write a code to take a string input and print the first and last capital letters in the string as appeared in the string

Example
input:
Hello this is a test A
output:
First capital = H Last capital = A

//Solution

`To be done by students`

#### Problem#3 (Exercise 6.10)

Write a program that will prompt the user to enter a hex digit character (“0″· … “9” or “A” … “F”), display it on the next line in decimal, and ask the user if he or she wants to do it again. If the user types “y” or “Y”, the ·program repeats; If the user types anything else, the program terminates. If the user enters an illegal character, prompt the user to try again.

## Example execution

1-1
2-2
A-10
B-11
F-15
x-i
z-i

//Solution

```top:
mov ah, 2
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
mov ah, 1
int 21h
mov bl, al

mov ah, 2
mov dl, '-'
int 21h

cmp bl, '0'
jl illegal
cmp bl, '9'
jg next

mov ah, 2
mov dl, bl
int 21h

jmp top

next:
cmp bl, 'A'
jl illegal
cmp bl, 'F'
jg illegal
mov ah, 2
mov dl, '1'
int 21h
sub bl, 'A'
mov dl, bl
int 21h
jmp top

illegal:
mov ah, 2
mov dl, 'i'
int 21h
jmp top```

#### Problem #4 (example 6.7)

Do the previous problem, except that if the user fails to enter a hex-digit character In three tries, display a message and terminate the program.

//Solution

```top:
mov ah, 2
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
mov ah, 1
int 21h
mov bl, al

mov ah, 2
mov dl, '-'
int 21h

cmp bl, '0'
jl illegal
cmp bl, '9'
jg next

mov ah, 2
mov dl, bl
int 21h

jmp top

next:
cmp bl, 'A'
jl illegal
cmp bl, 'F'
jg illegal
mov ah, 2
mov dl, '1'
int 21h
sub bl, 'A'
mov dl, bl
int 21h
jmp top

illegal:
MOV AH, 4CH
INT 21H```

### #Class lecture Lecture from lab doc 3 -> Logic instructions -> Shift instructions -> Example 7.1-7.13

#### Problem #5

Binary input

//Solution

`IN BOOK`

#### Problem #6

Binary output

//Solution

```mov bl, 'a'

mov cx, 8
top:
shl bl,1
jc print
mov ah, 2
mov dl, '0'
int 21h
jmp lbl

print:
mov ah, 2
mov dl, '1'
int 21h
lbl:
loop top

//Another approach

;mov bl, 'a'
xxi:
MOV ah, 1
int 21h
mov bl, al
sub bl, '0'
mov ah, 2
mov dl, 0dh
int 21h
mov dl, 0ah
int 21h
mov cx, 8
top:
shl bl,1
jc print
mov ah, 2
mov dl, '0'
int 21h
jmp lbl

print:
mov ah, 2
mov dl, '1'
int 21h
lbl:
loop top
jmp xxi
```

#### Problem #7

Hex input

//Solution

```xor bx, bx
mov cl, 4
mov ah, 1
int 21h
while:
cmp al, 0dh
je end_while
cmp al, 39h
jg letter

and al, 0fh
jmp shift
letter:
sub al, 37h
shift:
shl bx, cl
or bl, al
int 21h
jmp while
end_while:```

#### Problem #8

Hex output

//Solution

`In book`