PDA

View Full Version : [SOLVED] Assembly Language Loop Help


J8II
05-14-08, 04:44 AM
Hi y'all. I tried so many times to get this right and I couldnt do it. Can someone please help me.

Here is what the program is supposed to do. If the character is a decimal digit n then output the digit n on the next line n+1 times, then move the cursor to a new line and repeat step 1. If the character is a <CR> then exit (You may assume that the inputs are either a digit or <CR>)

An example of a program run is:

C>PROG2
?1
11
?3
3333
?5
555555
?0
0
?

C>

I know Im supposed to convert the digit from ASCII to a number using SUB BL,30H (at least I think it should be BL). I couldnt figure out where to insert it to get the program to work correctly. Also the second thing I need help with which I really couldnt figure out at all is using the while loop to loop the whole program unless it's a <CR>.

Now here is what I have:

.MODEL SMALL
.386
.STACK
.CODE
MAIN PROC
;display prompt
MOV AH,2 ;display character function
MOV DL,'?' ;character is '?'
INT 21H ;display it
;input a character
MOV AH,1 ;read character function
INT 21H ;character in AL
MOV BL,AL ;save it in BL
;go to a new line
MOV AH,2 ;display character function
MOV DL,0DH ;carriage return
INT 21H ;execute carriage return
MOV DL,0AH ;line feed
INT 21H ;execute line feed
;for loop
MOV CL,BL ;number of times to display from input
MOV AH,2 ;display character function
MOV AL,CL
TOP:
INT 21h ;display the number
LOOP TOP

MOV AH,4CH ;DOS exit function
INT 21H ;exit to DOS
MAIN ENDP
END MAIN

ShrpSight
05-20-08, 01:06 AM
I have taken Assembly before but it wasn't x86. Is this x86? If yes then I am quite sure there is no "LOOP" instruction.. you need to use Jump (JMP).

Also you are not decrementing the counter for how many times to display.. neither are you testing for it.

ShrpSight
05-20-08, 03:03 PM
Oops I was wrong in thinking there isn't a loop instruction. There is a loop instruction in x86, and by default it decrements the ECX register or the CX register depending on the address mode.

ShrpSight
05-27-08, 01:05 AM
well.. you marked it [SOLVED]. so what was the error..? and what did you fix?