Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Need help with 80x86

  1. #1
    ipride is offline Newbie
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Need help with 80x86

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Need help with 80x86

    Try writing it out in pseudocode first. How would you do it by hand?
    sudo rm -rf /

  4. #3
    ipride is offline Newbie
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Re: Need help with 80x86

    Quote Originally Posted by dargueta View Post
    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?

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Need help with 80x86

    You're very close:
    Code:
    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.)
    Last edited by dargueta; 02-28-2010 at 08:28 PM. Reason: Fixed little mistake.
    sudo rm -rf /

  6. #5
    ipride is offline Newbie
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Re: Need help with 80x86

    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

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Need help with 80x86

    add ah,ah ;multiply number by itself
    This is wrong. You want x^2, not x*2. You need to use the MUL instruction.

    mov eax, 0 ; exit with return code 0
    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.
    sudo rm -rf /

  8. #7
    ipride is offline Newbie
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Re: Need help with 80x86

    Quote Originally Posted by dargueta View Post
    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.
    ************************************************** ************************************************** ************************************************** ************************
    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

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Need help with 80x86

    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)

    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
    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 /

  10. #9
    ipride is offline Newbie
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Re: Need help with 80x86

    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

  11. #10
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Need help with 80x86

    A few problems you need to fix:

    Code:
         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:

    Code:
    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.

    Code:
        mov     eax,eax  ; exit
    You need to change this to XOR EAX, EAX.

    Code:
        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 /

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts