I need help with my assi. I don't even understand what the question is . Q. write a program that will input an integer valve and calculate the value of nBr where nBr is the smallest integer such that 1^2 + 2^2 + 3^2 +....... nBr^2 >= value
( these are square of one and two .....)
for example if you input 50, then the program would display 5 since 1+4+9+16 = 30, but 1+4+9+16+25= 55
15 replies to this topic
#1
Posted 26 February 2010 - 05:50 PM
|
|
|
#2
Posted 26 February 2010 - 10:13 PM
#3
Posted 27 February 2010 - 02:41 PM
dargueta said:
Try writing it out in pseudocode first. How would you do it by hand?
here is the design I came up with
Value: = 0;
number := 0;
While(value <= number)
Multiply number x number
Add 1 to number
Until (value == number);
does it look ok?
#4
Posted 27 February 2010 - 02:54 PM
You're very close:
Next step: assign variables to registers. (I highly discourage using any of the DX, EDX, RDX registers.)
value = 0;
number = 0;
numbertotal = 0;
while( 1 )
{
numbertotal += number * number;
if( numbertotal >= value )
return number;
else
++number;
}
Next step: assign variables to registers. (I highly discourage using any of the DX, EDX, RDX registers.)
Edited by dargueta, 28 February 2010 - 08:28 PM.
Fixed little mistake.
sudo rm -rf /
#5
Posted 28 February 2010 - 11:44 AM
thank you for all the help here is what my code looks like, please send any comments if you have.
.586 .MODEL FLAT INCLUDE io.h ; header file for input/output .STACK 4096 .DATA targetValue DWORD ? nbrTerms DWORD ? number DWORD ? prompt1 BYTE "Enter the integer value", 0 string BYTE 40 DUP (?) resultLbl BYTE "Number of terms", 0 nbrTerms BYTE 11 DUP (?), 0 .CODE _MainProc PROC input prompt1, string, 40 ; read ASCII characters atod string ; convert to integer dtoa targetValue, ecx ; convert to ASCII characters mov targetValue,0 ;value :=0 mov ecx,targetValue ;store targetValue in ecx mov nbrTerms,0 ;number of terms:=0 mov eax,nbrTerms ;store nbrTerms in eax mov number,0 ;number:=0 mov ah,number ;store number in ah whileB: cmp nbrTerms,1 ;nbrTerms==1 jne endwhileB ;exit if not equal add ah,ah ;multiply number by itself ifB: cmp ecx,eax ;comparing targetVale to nbrTerms jnle ifB ;exit if not targetVale<= nbrTerms inc eax ;add 1 increment++ jmp whileB ;repeat the while loop endloop: output resultLbl, nbrTerms ; output labeLb1 nbrTerms mov eax, 0 ; exit with return code 0 ret _MainProc ENDP END ; end of source code
#6
Posted 28 February 2010 - 12:35 PM
Quote
add ah,ah ;multiply number by itself
Quote
mov eax, 0 ; exit with return code 0
Instead of putting variables in memory, try to use registers if you can.
Also:
1) Since you never assign to nbrTerms after you declare it, it's going to go into an infinite loop.
2) There's no endWhileB label.
3) Why are you assigning 0 to a memory variable, assigning that to a register, and then using the registers?
4) You never write your results back to memory before outputting them. You're going to get unexpected results.
sudo rm -rf /
#7
Posted 28 February 2010 - 01:49 PM
dargueta said:
This is wrong. You want x^2, not x*2. You need to use the MUL instruction.
Use XOR EAX, EAX instead. Reduces code size and executes faster.
Instead of putting variables in memory, try to use registers if you can.
Also:
1) Since you never assign to nbrTerms after you declare it, it's going to go into an infinite loop.
2) There's no endWhileB label.
3) Why are you assigning 0 to a memory variable, assigning that to a register, and then using the registers?
4) You never write your results back to memory before outputting them. You're going to get unexpected results.
Use XOR EAX, EAX instead. Reduces code size and executes faster.
Instead of putting variables in memory, try to use registers if you can.
Also:
1) Since you never assign to nbrTerms after you declare it, it's going to go into an infinite loop.
2) There's no endWhileB label.
3) Why are you assigning 0 to a memory variable, assigning that to a register, and then using the registers?
4) You never write your results back to memory before outputting them. You're going to get unexpected results.
Ok I’m a little confused now,
1)Since you never assign to nbrTerms after you declare it, it's going to go into an infinite loop.
Does that mean I don’t have to declare it at all?
2. I assigned 0 to memory because I thought that we have to initialize targetValue to zero , same with nbrTerms = 0 that means I don’t have to add the lines
mov targetValue,0
mov nbrTerms,0
this part I didn’t get it
4) You never write your results back to memory before outputting them. You're going to get unexpected results
#8
Posted 28 February 2010 - 02:15 PM
Ok, I'll explain:
1) You only have to declare it if output cannot take a register as its second argument. Otherwise it's useless, as are targetValue and number, as well as the first nbrTerms you declared. (I just noticed you declared it twice.)
2) If you push these variables into registers you won't have to use memory. Memory is slooow. Take my word for it.
4)
1) You only have to declare it if output cannot take a register as its second argument. Otherwise it's useless, as are targetValue and number, as well as the first nbrTerms you declared. (I just noticed you declared it twice.)
2) If you push these variables into registers you won't have to use memory. Memory is slooow. Take my word for it.
4)
input prompt1, string, 40 ; read ASCII characters
...
[COLOR="GREEN"] mov nbrTerms,0 ;number of terms:=0[/COLOR]
...
whileB:
[COLOR="RED"] cmp nbrTerms,1 ;nbrTerms==1
jne endwhileB ;exit if not equal
[/COLOR]
add ah,ah ;multiply number by itself
ifB:
cmp ecx,eax ;comparing targetVale to nbrTerms
jnle ifB ;exit if not targetVale<= nbrTerms
inc eax ;add 1 increment++
jmp whileB ;repeat the while loop
endloop:
[COLOR="BLUE"] output resultLbl, nbrTerms ; output labeLb1 nbrTerms[/COLOR]
mov eax, 0 ; exit with return code 0
ret
Notice that you assign nbrTerms to 0 (green) and then never assign it again. This means that it will always be 0, and the exit condition for your loop (red) will never execute. So you'll never exit your loop. Even if by magic you break out of the loop, your output (blue) will always be 0.
sudo rm -rf /
#9
Posted 28 February 2010 - 07:33 PM
thank you again I made some changes to the code but I'm still running to errors.
.586 .MODEL FLAT INCLUDE io.h ; header file for input/output .STACK 4096 .DATA targetValue DWORD ? nbrTerms DWORD ? number DWORD ? prompt1 BYTE "Enter the integer value", 0 string BYTE 40 DUP (?) resultLbl BYTE "Number of terms", 0 nbrTerms BYTE 11 DUP (?), 0 .CODE _MainProc PROC input prompt1, string, 40 ; read ASCII characters atod string ; convert to integer dtoa targetValue, ecx ; convert to ASCII characters mov ecx,targetValue ;store targetValue in ecx mov eax,nbrTerms ;store nbrTerms in eax mov ah,number ;store number in ah whileB: cmp nbrTerms,1 ;nbrTerms==1 jne endwhileB ;exit if not equal mul ah,ah ;multiply number by itself ifB: cmp ecx,eax ;comparing targetVale to nbrTerms jnle ifB ;exit if not targetVale<= nbrTerms inc eax ;add 1 increment++ jmp whileB ;repeat the while loop endwhilB: output resultLbl, nbrTerms ; output labeLb1 nbrTerms mov eax,eax ; exit ret _MainProc ENDP END ; end of source code
#10
Posted 28 February 2010 - 08:25 PM
A few problems you need to fix:
This is why I hinted earlier that you shouldn't use any of the *DX registers.
Plus, you're storing nbrTerms into EAX, and then storing number into AH. AH is part of EAX, so you're overwriting your own data. You need to use a different register.
And you still have the "never writing to nbrTerms" error.
mul ah,ah ;multiply number by itself
Illegal. MUL only takes one operand, the accumulator (AL, AX, EAX) is the other operand you're multiplying by. Allow me to demonstrate:mul cl ; al * cl, ax contains result mul cx ; ax * cx, dx:ax contains result mul ecx ; eax * ecx, edx:eax contains result
This is why I hinted earlier that you shouldn't use any of the *DX registers.
mov eax,eax ; exit
You need to change this to XOR EAX, EAX.
mov ecx,targetValue ;store targetValue in ecx
mov eax,nbrTerms ;store nbrTerms in eax
mov ah,number ;store number in ah
If these comments are correct, you've got the operands flipped. You need MOV number, AH and so on. Either that or the operands are in the right order but your comments are wrong.Plus, you're storing nbrTerms into EAX, and then storing number into AH. AH is part of EAX, so you're overwriting your own data. You need to use a different register.
And you still have the "never writing to nbrTerms" error.
sudo rm -rf /
#11
Posted 28 February 2010 - 09:15 PM
this is harder than what thought thank you for all the help I'm not sure why they assigning a problem like this if it's only intro to asseembly. I made some change to assigning registers.
.586 .MODEL FLAT INCLUDE io.h ; header file for input/output .STACK 4096 .DATA targetValue DWORD ? number DWORD ? prompt1 BYTE "Enter the integer value", 0 string BYTE 40 DUP (?) resultLbl BYTE "Number of terms", 0 nbrTerms BYTE 11 DUP (?), 0 .CODE _MainProc PROC input prompt1, string, 40 ; read ASCII characters atod string ; convert to integer dtoa targetValue, cl ; convert to ASCII characters mov cl,targetValue ;store targetValue in cl mov cx,nbrTerms ;store nbrTerms in cx mov number,ah ;store number in ah whileB: cmp nbrTerms,1 ;nbrTerms==1 jne endwhileB ;exit if not equal mul cl ;multiply number by itself ifB: cmp cl,cx ;comparing targetVale to nbrTerms jnle ifB ;exit if not targetVale<= nbrTerms inc ah ;add 1 increment++ jmp whileB ;repeat the while loop endwhilB: output resultLbl, nbrTerms ; output labeLb1 nbrTerms xor eax,eax ; exit ret _MainProc ENDP END ; end of source code
#12
Posted 28 February 2010 - 09:22 PM
mov cl,targetValue ;store targetValue in cl mov cx,nbrTerms ;store nbrTerms in cxTwo things wrong with this:
1) targetValue and nbrTerms are declared as DWORDs. This means you must use one of the 32-bit registers, e.g. EAX, EBX, ECX, etc. to hold them.
2) You're doing the same thing again with CL/CX - overwriting your own data.
You also misspelled endwhileB in your exit label.
sudo rm -rf /
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









