简体   繁体   中英

how to compare two strings, and change the color of them in assembly 8086

can someone please help me, I'm knew to assembly, and I have to do the following:

Write an assembly language program that allows you to enter two lines of no more than 50 characters from the keyboard. and displays the following groups of characters in the appropriate color: common characters for both terms (in red), characters of the first line that are not in the second (in blue), characters of the second line that are not in the first (in yellow)

I already did the first part, but I can't Feger out how to change the text color

.model small

.data
message1 db "Enter any string: $"
message2 db "Given string is :$"
str1 db 50 dup('$')
str2 db 25 dup('$')

.code

mov ax,@data
mov ds,ax   
;--------------------------------------------------
; disply first massege:
mov dx,offset message1    ; Enter any string: 
mov ah,09h
int 21h    
;adding new line
;--------------------------------------------------
call new_line
;--------------------------------------------------
;fist strig up to 25 char
firstString:
lea dx, str1
mov str1,26
mov ah,10
int 21h

;adding new line
;--------------------------------------------------
call new_line 
;--------------------------------------------------   
;second string up to 25 char
lea dx, str2
mov str2,26
mov ah,10
int 21h

;adding new line
;--------------------------------------------------
call new_line
        
;disply the input
;-------------------------------------------------- 
mov dx,offset message2
mov ah,09h
int 21h 

;adding new line
;--------------------------------------------------
call new_line
;first string
;------------
mov cl,str1[1]
mov si,2
Output:
mov dl,str1[si]
mov ah,2
int 21h
inc si
loop output        
;adding new line
;--------------
call new_line
;second string
;-------------
mov cl,str2[1]
mov si,2
Output2:
mov dl,str2[si]
mov ah,2
int 21h
inc si
loop output2         
exit:


mov ah,4ch
int 21h

new_line proc near
mov dl,10
mov ah,2
int 21h
mov dl,13
mov ah,2
int 21h   
ret 
new_line endp   


end

I tried to use this code, but it did work:

mov si, 2
mov ch, 25 ;I have the max 25 char so I figured that I need to loop 25 times
color:
MOV AH,09         ; FUNCTION 9
MOV AL,str1[si] ; here i think i need to move my first element 
MOV BX,0004      ; PAGE 0, COLOR 4
MOV Cl,1 ; here I don't know how many elements I have because i take input from the user 
inc si
INT 10H              ; INTERRUPT 10 -> BIOS
INT 20H 
loop color             ; END

Your attempt is going in the right direction but has some flaws.

  • Your loop color instruction depends on the whole 16-bit register CX, therefore setting mov ch, 25 is not enough! Use mov cx, 25 .
  • Since your loop is controlled by the CX register, you cannot use CL internally for something else unless you preserve the value somehow .
  • The INT 20H instruction does not belong in this loop at all!
  • The output with color does not advance the cursor. You need to do that additionally. A simple way is to use the BIOS.Teletype function 0Eh.
  mov  si, 2
  mov  cx, 25
  mov  bx, 0004h        ; Page 0, Color RedOnBlack
color:
  push cx               ; (1) Preserve CX
  mov  cx, 1            ; Repetition count
  mov  al, str1[si]
  inc  si
  mov  al, 09h
  int  10h              ; INTERRUPT 10 -> BIOS
  mov  ah, 0Eh
  int  10h
  pop  cx               ; (1) Restore CX
  loop color

An alternative that does not need to preserve CX as we can use DX to control the loop:

  mov  si, 2
  mov  dx, 25
  mov  cx, 1            ; Repetition count
  mov  bx, 0004h        ; Page 0, Color RedOnBlack
color:
  mov  al, str1[si]
  inc  si
  mov  al, 09h
  int  10h              ; INTERRUPT 10 -> BIOS
  mov  ah, 0Eh
  int  10h
  dec  dx
  jnz  color

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM