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
Try writing it out in pseudocode first. How would you do it by hand?
sudo rm -rf /
You're very close:
Next step: assign variables to registers. (I highly discourage using any of the DX, EDX, RDX registers.)Code:value = 0; number = 0; numbertotal = 0; while( 1 ) { numbertotal += number * number; if( numbertotal >= value ) return number; else ++number; }
Last edited by dargueta; 02-28-2010 at 08:28 PM. Reason: Fixed little mistake.
sudo rm -rf /
thank you for all the help here is what my code looks like, please send any comments if you have.
HTML Code:.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
This is wrong. You want x^2, not x*2. You need to use the MUL instruction.add ah,ah ;multiply number by itself
Use XOR EAX, EAX instead. Reduces code size and executes faster.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 /
************************************************** ************************************************** ************************************************** ************************
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
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)
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.Code:input prompt1, string, 40 ; read ASCII characters ... mov nbrTerms,0 ;number of terms:=0 ... 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
sudo rm -rf /
thank you again I made some changes to the code but I'm still running to errors.
HTML Code:.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
A few problems you need to fix:
Illegal. MUL only takes one operand, the accumulator (AL, AX, EAX) is the other operand you're multiplying by. Allow me to demonstrate:Code:mul ah,ah ;multiply number by itself
This is why I hinted earlier that you shouldn't use any of the *DX registers.Code:mul cl ; al * cl, ax contains result mul cx ; ax * cx, dx:ax contains result mul ecx ; eax * ecx, edx:eax contains result
You need to change this to XOR EAX, EAX.Code:mov eax,eax ; exit
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.Code:mov ecx,targetValue ;store targetValue in ecx mov eax,nbrTerms ;store nbrTerms in eax mov ah,number ;store number in ah
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 /
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks