sickly_man 0 Light Poster

hello everybody. the project im working on asks me to propmt the user once to change the background color and once to change the foreground (text) color. right now my program prompts the user...but not with the message i have in the data segment...it spits out some unattractive code. could some of you kind assembly programmers take a look at my code? IM NOT ASKING THAT YOU DO MY ASSIGNMENT FOR ME! ive done work, i am just stuck on the keyboard input part....im doing something wrong with displaying my prompt and getting keyboard input. thanks in advance. my code is below.

and btw im using a TASM compiler and TLINK linker and running this in the Windows command prompt.

TITLE	Homework 7 attempt # 2
;............................................................
STACKSG	SEGMENT PARA STACK 'Stack'
DB 32 dup(0)			;32 words, might want to set larger
STACKSG	ENDS
;.................................................................
DATASG SEGMENT PARA 'Data' 	;data segment
	paralst	LABEL	BYTE	
	maxlen	db	10
	actlen	db	?
	dbdata	db	10	DUP('$')
	prompt1	DB	'Enter a foreground color:$', '$'
	prompt2	DB	'Enter a background color:$', '$'

DATASG ENDS
;.............................................................
CODESG SEGMENT PARA 'Code'
MAIN	 PROC FAR		;main procedure
	ASSUME SS:STACKSG, DS:DATASG, CS:CODESG
	MOV AX,DATASG		;Set address of data
	MOV DS,AX		;Segment in DS


		; CODE GOES HERE!!!!

		; CHANGE OUTER BACKGROUND COLOR
		mov	ah, 06H
		mov 	al, 0
		mov	bh, 10H
		mov	cx, 0000H
		mov	dx, 184FH
		int	10H

		; CHANGE INNER BACKGROUND COLOR
		mov	ah, 06H
		mov 	al, 0
		mov	bh, 70H
		mov	cx, 060FH
		mov	dx, 1240H
		int	10H

		; SET CURSOR POSITOIN
		mov ah, 02H
		mov bh, 00
		mov dh, 09
		mov dl, 30
		int 10h

		; DISPLAY PROMPT TO USER
		mov ah, 09H
		lea bx, prompt1
		int 21H

		; GET KEYBOARD INPUT
		mov	ah, 0ah
		lea	dx, paralst
		int	21h

		;check if no characters entered
		lea	dx, actlen
		cmp	actlen, 0
		je	again

		; CHECK FOR A CHAR CODE
		lea	dx, dbdata

		; CHECK FOR Red
		cmp	dx, 72h		;lower case r
		je	code_R

		;check for Green
		cmp	dx, 67h		;lower case g
		je	code_G

		jmp again	; dont change color

		;to clear screen and set background to red
	code_R:	mov	ah, 06h
		mov 	al, 0
		mov	bh, 40h
		mov	cx, 060Fh
		mov	dx, 1240h
		int	10h

		;to clear screen and set background to green
	code_G:	mov	ah, 06h
		mov 	al, 0
		mov	bh, 20h
		mov	cx, 060Fh
		mov	dx, 1240h
		int	10h

	again:	jmp again

	MOV AX,4C00H		;End processing
	INT 21H
MAIN	ENDP			;End procedure
CODESG	ENDS			;End segment
	END MAIN		;End program

Dani AI

Generated

— the symptom (a mangled prompt and wrong comparisons) points to three concrete mistakes: the DOS print call needs DS:DX, the buffered-input layout for AH=0Ah must be accessed correctly, and the code is comparing addresses instead of the stored characters.

Key points and a compact fix:

  • INT 21h AH=09 prints a $-terminated string using DS:DX. Loading the offset into BX (LEA BX, ...) will not work. Use DX for that call.
  • INT 21h AH=0Ah expects a buffer structured as: byte 0 = max length, byte 1 = actual length (set by DOS), bytes 2.. = characters typed (no CR). Read the characters at buffer+2.
  • Comparisons must use the byte read from the buffer (e.g. [buf+2]), not the offset register or label.

Example (TASM/MASM-style) showing only the corrected I/O and checks (variable names intentionally different from the original post):

prompt_fg  db 'Enter foreground (r/g):$',0
inbuf      db 10        ; maxchars
           db 0         ; actual length (filled by DOS)
           db 10 dup(0) ; storage

; print prompt
    mov dx, OFFSET prompt_fg
    mov ah, 09h
    int 21h

; read line (buffered)
    lea dx, inbuf
    mov ah, 0Ah
    int 21h

; check length and get first char
    mov al, [inbuf+1]       ; actual length
    cmp al, 0
    je  no_input
    mov al, [inbuf+2]       ; first character
    cmp al, 'r'
    je  set_red
    cmp al, 'R'
    je  set_red
    cmp al, 'g'
    je  set_green
    cmp al, 'G'
    je  set_green

Troubleshooting notes: confirm DS points to the data segment before calling INT 21h; use both uppercase and lowercase checks or convert the case; avoid infinite jmp loops while debugging. Also, modern 64‑bit Windows cannot run 16‑bit DOS interrupts—use DOSBox or a VM when testing this code on newer systems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.