Assembly Language: How to make a loop skip the first line?

AI Thread Summary
The discussion centers around a problem with assembly code intended to clear the DOS screen and display a user-input string at the top. The user initially attempts to use a loop to fill the screen with spaces (ASCII 20H) but inadvertently clears their message as well. Suggestions include using interrupt calls to clear the screen and print the message separately, rather than trying to manage the screen clearing manually. The importance of understanding the buffer structure when reading input is emphasized, noting that the first byte indicates the maximum number of bytes to read, while the second byte reflects the actual bytes read. There are also comments about the naming conventions for variables and labels, stressing the need for meaningful names to enhance code readability and maintainability. The user expresses frustration with their limited knowledge of assembly language, but the responses encourage them to continue learning and improving their understanding of coding concepts.
mimzy
Messages
6
Reaction score
0
Hello, so i have a problem with my assembly code. I want to clear the screen of the DOS and display the string input in the first line. I decided to make a loop that will run 2000 times and fill the space with [20H, which is the ascii for space bar] however, It clears out my message as well and I thought of skipping the first 40 loops so that it'll leave my string[max 40 chars] as it is and proceed to clear my screen; however, I've tried many ways but I can't get it right D: here's here code i have atm

MOV DX,OFFSET BUF1 ; OFFSET=STORE IN THE SAME FLOOR "BUF1"
MOV BUF1,40 ; MAX CHARACTERS IN BUF1 = 40

MOV AH,0AH ; READS ONE LINE FROM KEYBOARD
INT 21H

[this is not relevant, it just creates, writes, reads, and closes a file lol :P but i thought it might help a little D:]
MOV AH,3CH ; CREATION 3CH
XOR CX,CX
MOV DX,OFFSET FILE1
INT 21H ; CREATION FILE END

MOV BX,AX
MOV AH,40H ; WRITING 40H
MOV CX,40 ; MAX WRITING SIZE = 40
MOV DX,OFFSET BUF1
INT 21H ; WRITING FILE END

MOV AH,3EH
INT 21H ; CLOSE FILE

MOV AH,3DH ; OPEN 3DH
MOV AL,0
MOV DX,OFFSET FILE1
INT 21H ; OPEN FILE END
MOV BX,AX

MOV AH,3FH ; READING 3FH
MOV CX,40 ; MAX CHARACTERS READ = 40
MOV DX,OFFSET BUF2
INT 21H ; READING FILE END

MOV AH,3EH ; CLOSE 3EH
INT 21H ; CLOSE FILE END
MOV DX,OFFSET BUF2
MOV AH,0AH
INT 21H
MOV CX,2000 ; START OF CLEARING RUN LOOP 2000 TIMES

LUCY: MOV AH,06H ; START OF LOOP, CALL DISPLAY FUNCTION 06H
MOV DL,20H ; 20H=' ' [SPACE IN ASCII]
INT 21H
LOOP LUCY ; LOOP END

I know Assembly Code isn't very popular :( that's why it's so hard to find a solution for my problem Q.Q and i have to seek for help here. If no one knows it, ill understand :c

thanks :D
 
Technology news on Phys.org
Hi mimzy
You are making your life unnecessarily hard :smile:
You are in a DOS box, so the right way to do it is to
1) clear the string with an interrupt call
2) print your message with another one

Here is a list of interrupt calls you might find useful
 
well the thing is that i need to print the string on the first line of the dos... so i was thinking of printing it and then clearing it; however, when I clear it, it clears from the beginning... so yeh. Also, I need to use this method since it's the only way our professor has shown us so far D:
 
mimzy said:
Hello, so i have a problem with my assembly code. I want to clear the screen of the DOS and display the string input in the first line. I decided to make a loop that will run 2000 times and fill the space with [20H, which is the ascii for space bar] however, It clears out my message as well and I thought of skipping the first 40 loops so that it'll leave my string[max 40 chars] as it is and proceed to clear my screen; however, I've tried many ways but I can't get it right D: here's here code i have atm

MOV DX,OFFSET BUF1 ; OFFSET=STORE IN THE SAME FLOOR "BUF1"
MOV BUF1,40 ; MAX CHARACTERS IN BUF1 = 40

MOV AH,0AH ; READS ONE LINE FROM KEYBOARD
INT 21H

Run your loop to print a space character at each position of the window, and then print your input string. I don't understand why clearing the screen should also clear your input string.

In the first line of code above, you have this comment: OFFSET=STORE IN THE SAME FLOOR "BUF1". What does that mean, particularly "in the same floor"?

You also need to keep in mind that the 0Ah function of int 21h returns a buffer with the first byte being the number of bytes to read, and the second byte being the number of bytes actually read. You probably don't want to print those two bytes on the screen.
 
If you want the line at the top of the screen, then why not write the line, followed by hex 0D (carriage return), then followed by 19 hex 0A (line feed)? This will end up putting the cursor at the bottom of the screen though (unless you turn the cursor off, or use other instructions to reset the cursor location).
 
mimzy said:
MOV DX,OFFSET BUF2
MOV AH,0AH
INT 21H
MOV CX,2000 ; START OF CLEARING RUN LOOP 2000 TIMES

LUCY: MOV AH,06H ; START OF LOOP, CALL DISPLAY FUNCTION 06H
MOV DL,20H ; 20H=' ' [SPACE IN ASCII]
INT 21H
LOOP LUCY ; LOOP END

A couple of comments on your code...

1. Why do you have BUF2?
2. Your label LUCY should be something more meaningful in relation to your program, not the name of a friend or girlfriend or whatever.
 
Mark44 said:
Run your loop to print a space character at each position of the window, and then print your input string. I don't understand why clearing the screen should also clear your input string.

In the first line of code above, you have this comment: OFFSET=STORE IN THE SAME FLOOR "BUF1". What does that mean, particularly "in the same floor"?

You also need to keep in mind that the 0Ah function of int 21h returns a buffer with the first byte being the number of bytes to read, and the second byte being the number of bytes actually read. You probably don't want to print those two bytes on the screen.

it does show once the screen is cleared with the loop and then print the string but the assignment was to print the string on the first line so I'm just trying to stick to what I've been asked to be done D: although it does work in the way u described.

the comment i made on the offset was because someone had told me that it meant to store a set of data let's say, in the same "floor" as buf1, as in the data will not be scattered around... It doesn't rly have much to do with the line of code next to it, it was just to remind me of what offset meant :P unless that person gave me incorrect info xD

and lastly, I'm not quite sure what u meant but i think u mean the 1 byte of space that stores the max number of bytes and the other 1 byte that stores the min number of bytes? if so then both buf1&buf2 are given the value of 42 in the data segment o.o ... i think i should have posted the entire code here Dx my bad :(

rcgldr said:
If you want the line at the top of the screen, then why not write the line, followed by hex 0D (carriage return), then followed by 19 hex 0A (line feed)? This will end up putting the cursor at the bottom of the screen though (unless you turn the cursor off, or use other instructions to reset the cursor location).

hmm that makes sense o,o but how can I make the loop skip that line if the loop starts looping [clearing with space] from the very first line of the command prompt D:?

Mark44 said:
A couple of comments on your code...

1. Why do you have BUF2?
2. Your label LUCY should be something more meaningful in relation to your program, not the name of a friend or girlfriend or whatever.

oh.. well I was told[again lol] that since this program needed to be closed and then read, we had to use a different buffer to read it? Dx
oh and and loop name was my real name mimi but i felt kinda weird posting it up with my name so i took an anime character's name instead XD (kinda silly XD but i'll follow ur suggestion next time ^-^)

Thanks a lot for all ur responses! and sorry for my ignorance about assembly language, my class and I have only covered so much Dx I feel kinda embarrassed for not having enough knowledge to answer ur questions about my own code :(
 
mimzy said:
it does show once the screen is cleared with the loop and then print the string but the assignment was to print the string on the first line so I'm just trying to stick to what I've been asked to be done D: although it does work in the way u described.

the comment i made on the offset was because someone had told me that it meant to store a set of data let's say, in the same "floor" as buf1, as in the data will not be scattered around...
I still don't get what you mean by "floor." A floor is a surface in a building that you walk on, and I don't see how that connects with computer memory.

mimzy said:
It doesn't rly have much to do with the line of code next to it, it was just to remind me of what offset meant :P unless that person gave me incorrect info xD
Maybe they were pulling your leg.

You should try to get into the practice of putting in comments that are related to the code next to them. It's fine to put in something to help you remember what you're doing, but it should be related to the code in some way.
mimzy said:
and lastly, I'm not quite sure what u meant but i think u mean the 1 byte of space that stores the max number of bytes and the other 1 byte that stores the min number of bytes? if so then both buf1&buf2 are given the value of 42 in the data segment o.o ... i think i should have posted the entire code here Dx my bad :(
The first byte is the number of bytes you want to read, at most; the second byte is put there by DOS, and it is the number of bytes it read.

You'll confuse yourself if you try to use two buffers to input one string of characters, so you should get rid of BUF2.
mimzy said:
hmm that makes sense o,o but how can I make the loop skip that line if the loop starts looping [clearing with space] from the very first line of the command prompt D:?
I don't know why you need to. What you said in your first post was "I want to clear the screen of the DOS and display the string input in the first line." Why do you think you need to skip over some text?
mimzy said:
oh.. well I was told[again lol] that since this program needed to be closed and then read, we had to use a different buffer to read it? Dx
I would stop listening to whoever is telling you this stuff.
mimzy said:
oh and and loop name was my real name mimi but i felt kinda weird posting it up with my name so i took an anime character's name instead XD (kinda silly XD but i'll follow ur suggestion next time ^-^)
It doesn't matter if it was your name or some cartoon character - it's not a good name for a label. Your instructor is liable to deduct points for using a meaningless (and therefore silly) label name.

The biggest reason for choosing variable names and labels and writing meaningful comments is to help whoever comes along next understand your program. They also help you understand you code better if you look at it a few days, weeks, or months after you wrote it. If you want to appear professional, don't choose dumb names for variables, PROCs, labels, etc.
mimzy said:
Thanks a lot for all ur responses! and sorry for my ignorance about assembly language, my class and I have only covered so much Dx I feel kinda embarrassed for not having enough knowledge to answer ur questions about my own code :(

Don't feel bad - assembly language is hard, and it's OK to feel ignorant about something when you're trying to learn it! There are advantages, though, one of which is that you get an intimate understanding of what is happening in the computer's CPU.
 
Back
Top