954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Basic Loop in Assembly

Ola!

I'm still kind of new with the whole assembly language and just need some help with this program i'm writing.

What I want it to do is to ask a question -like for a number and then store that inserted number in a variable. And then test whether its bigger than 14 which I declared as a variable already as shown below-and then there is a loop to ask again what is the number. So I want the loop to run as many time the number the user entered.

output db 10,13, "Enter your number: $"
asq dw 10,13, "$" ; ascii values for a new line
stilltoimplement db 10,13, "Do calculations later on $"

.code 
    
jmp start 

    number db ?
    max db 14 ;max number
    
start:
    mov ax,@data 
    mov ds,ax
    
    mov ah,09 ;prints new line
    mov dx, offset output
    int 21h
    
    mov ah, 01 ;checks for key
    int 21h
    mov number, al
    
    cmp max,number
    jg start
    jl part2

part2:

    mov ah,09
    mov dx,offset stilltoimplement
    int 21h
    
ending:
    mov ah,4ch
    mov al,00
    int 21h ;End the program
    
END


It gives me an error in where I try to compare the two variables and says Illegal memory reference. Prob just something small

Any help will be appreciated!

Thanks

Phillamon
Junior Poster in Training
65 posts since Oct 2010
Reputation Points: 36
Solved Threads: 5
 

If the user enters 14, will it ask 14 times or will that be a different question to ask?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

Ola!

I'm still kind of new with the whole assembly language and just need some help with this program i'm writing.

What I want it to do is to ask a question -like for a number and then store that inserted number in a variable. And then test whether its bigger than 14 which I declared as a variable already as shown below-and then there is a loop to ask again what is the number. So I want the loop to run as many time the number the user entered.

output db 10,13, "Enter your number: $"
asq dw 10,13, "$" ; ascii values for a new line
stilltoimplement db 10,13, "Do calculations later on $"

.code 
    
jmp start 

    number db ?
    max db 14 ;max number
    
start:
    mov ax,@data 
    mov ds,ax
    
    mov ah,09 ;prints new line
    mov dx, offset output
    int 21h
    
    mov ah, 01 ;checks for key
    int 21h
    mov number, al
    
    cmp max,number
    jg start
    jl part2

part2:

    mov ah,09
    mov dx,offset stilltoimplement
    int 21h
    
ending:
    mov ah,4ch
    mov al,00
    int 21h ;End the program
    
END

It gives me an error in where I try to compare the two variables and says Illegal memory reference. Prob just something small

Any help will be appreciated!

Thanks


In line 24..
You can not use cmp with both operands being a memory variable. Instead use an immediate value

cmp number,14d  ;the immediate value must be the second operand

or you can also do

cmp al,max
diwakar wagle
Posting Whiz in Training
203 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
 

Well, if i undersood corectly, you want to loop that the user puts how many times, so, you can use the register ECX to store how many times you want the loop and then use the LOOP procedure.

AceStryker
Light Poster
47 posts since Aug 2011
Reputation Points: 10
Solved Threads: 2
 

I just wanted to test the cmp value. The Loop is actually for something else I just couldn't understand why it doesn't compare.

Thanks diwakar wagle it makes sense to me now!

And yes that is correct AceStryker!

Thanks for the help guys! After a day of searching I got myself a nice assembly online tutor which helped me alot!

Phillamon
Junior Poster in Training
65 posts since Oct 2010
Reputation Points: 36
Solved Threads: 5
 
MOV ECX, 10;number of times the loop will execute
LOOP INLINEFUNC
INLINEFUNC:
AceStryker
Light Poster
47 posts since Aug 2011
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: