Thread: Assembly Mayhem !!!!

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question ChInChiLLA MaDnESs !!!

    Need your guys's opinion on how to get this program woikin'

    IA-32 Assembly
    Code:
    TITLE Chinchilla Madness
    
    ;this program will calculate population growth of
    ;the wild chinchilla over a 14 year span
    
    INCLUDE Irvine32.inc
    
    counter = 12    ;this will start counter on the 3rd element of the array
    
    .data
    
    prompt1 BYTE  "Enter semi-annual rate of chinchilla offspring",0
    prompt2 BYTE  "Total number of chinchillas at a rate of ",0
    prompt3 BYTE  " chinchillas semi-annually is: ",0
    array   DWORD 112 DUP(?)  ;14 years..  semi-annually = 28, multiplied by 4 (for 32bit array) = 112
    
    .code
    main PROC
    
    	call Clrscr
    	mov esi, OFFSET array
    	mov ecx, 27
    	call RatePrompt
    	call ChinchillaCalculate
    	call DisplayTotal
    	exit
    
    main ENDP
    
    
    
    RatePrompt PROC
    
    	mov edx, OFFSET prompt1
    	call WriteString
    	call ReadInt
    	
    	ret
    
    RatePrompt ENDP
    
    
    
    
    
    
    ChinchillaCalculate PROC
    
    	mov [esi],   2  ;first and second elements will always
    	mov [esi+4], 2  ;be set to '2' (representing the parents)
    
    L1: 
    
         
            ;This algorithm represents, "go back two elements from the
            ;current element, and divide it's contents by '2'..  then 
            ;multiply the resultant by the user entered rate (in eax) then 
            ;add to the contents in [current element - 1]...   and put
            ;the final result in [current element]
    
    	subcounter = counter	
    
    	sub subcounter, 4  ;address of [current element - 1]
    	mov eax, subcounter
    	sub subcounter, 4  ;address of [current element - 2]
    	mov ebx, subcounter
     
    	mov edx, esi
    	sub edx,bx
    	div edx, 2
    	mul edx, eax
    	add edx, [esi-ax]	
    	
    	add counter, 4
            loop L1
    
    	ret
    
    ChinchillaCalculate ENDP
    
    
    
    
    
    
    
    
    DisplayTotal PROC
    
    	mov edx, OFFSET prompt2
    	call WriteString
    	call WriteInt
            mov edx, OFFSET prompt3 
            call WriteString
    	move eax, [esi+112]
    	call WriteInt
    
    	ret
    
    DisplayTotal ENDP
    
    END main

    Here are the specific error messages... brought to you by the MASM615 assembler:


    Microsoft (R) Macro Assembler Version 6.15.8803
    Copyright (C) Microsoft Corp 1981-2000. All rights reserved.

    Assembling: chinchilla.asm
    chinchilla.asm(50) : error A2070: invalid instruction operands
    chinchilla.asm(51) : error A2070: invalid instruction operands
    chinchilla.asm(57) : error A2001: immediate operand not allowed
    chinchilla.asm(59) : error A2001: immediate operand not allowed
    chinchilla.asm(64) : error A2008: syntax error : ,
    chinchilla.asm(65) : error A2008: syntax error : ,
    chinchilla.asm(70) : error A2001: immediate operand not allowed
    chinchilla.asm(85) : error A2008: syntax error : in instruction
    chinchilla.asm(88) : error A2008: syntax error : eax
    chinchilla.asm(63) : error A2022: instruction operands must be the same size
    chinchilla.asm(66) : error A2032: invalid use of register
    Press any key to continue . . .


    I haven't coded that much in assembly.. i think the code looks good... just need some help on convincing the compiler it's good


    Help the assembly noob.
    Last edited by The Brain; 09-17-2004 at 05:56 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>INCLUDE Irvine32.inc

    It would be easier if you could provide a more simple example not relying on headers which most people wont have

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb

    Well.. after days of troubleshooting.. I got me' program to work!!!

    My main problems were:

    the edx register needs to be cleared when using the 'div' operator.. because division uses both the edx:eax registers as the dividend. setting edx to zero will eliminate garbage data from any previous operations using this register.

    also, I used a different approach to array indexing.. instead of continually manipulating the array offset in the esi register, I used a style of array indexing.. that resembles more of what you would see in a high-level language:
    Code:
    array[ebx]
    where the ebx is a dedicated array counter. This style is better, because one does not have to increment an array pointer by units of 4 (in a 32-bit array).. there is now a 1:1 relationship between the array counter and the array address.. (just like with a high level language )

    Anywhoo.. Here is the finished product Let me know what you think!! Feedback = good

    i'll also be putting this up on flashdaddy.com (thanks axon)

    Code:
    TITLE Chinchilla Madness
    
    ;this program will calculate population growth of
    ;the wild chinchilla over a 14 year span
    
    INCLUDE Irvine32.inc
    
    
    .data
    
    prompt1 BYTE  "Enter semi-annual rate of chinchilla offspring: ",0
    prompt2 BYTE  "Total number of chinchillas at a rate of ",0
    prompt3 BYTE  " chinchillas semi-annually is: ",0
    array   DWORD 28 DUP(?)  ;14 years..  semi-annually = 28
    loopcount	DWORD ?
    rate		DWORD ?
    
    .code
    main PROC
    
    	mov esi, 0
    	call Clrscr
    	mov esi, OFFSET array
    	mov ecx, 26
    	call RatePrompt		  		
    	call ChinchillaCalculate
    	call DisplayTotal
    	exit
    
    main ENDP
    
    
    
    RatePrompt PROC
    
    	mov edx,0
    	mov edx, OFFSET prompt1
    	call WriteString
    	call ReadInt
    	mov rate, eax
    
    	ret
    
    RatePrompt ENDP
    
    
    
    
    
    
    ChinchillaCalculate PROC
    
    	
    	mov array[0], 2 ;first and second elements will always
    	mov array[1], 2 ;be set to '2' (representing the parents)
    	mov ebx, 2	;ebx will be my dedicated array counter (starting on 3rd element)
    	
    
    L1: 
         
            ;This algorithm represents, "go back two elements from the
            ;current element, and divide it's contents by '2'..  then 
            ;multiply the quotient by the user entered rate.. then 
            ;add to the contents in [current element - 1]...   and put
            ;the final result in [current element]
    	
    	
    	mov loopcount, ecx		;free up ecx for use in calculations	
    	
        	mov eax, array[ebx-2]	 	;"two elements back from current element" mov to eax in preperation for divide
    	mov ecx, 2			;use ecx as register operand for 'mul' and 'div'
    	mov edx, 0			;clear edx to perform low-end division 
    	div ecx				;divide eax/2 and store quotient into eax	
    	mov edx, 0			;throw away any remainder
    	mov ecx, rate			;move rate to ecx as a register operand for use with 'mul'
    	mul ecx				;multiply answer (in eax) by rate and store in edx:eax
    	add eax, array[ebx-1] 		;"add result to [current element - 1]"
    	mov array[ebx], eax		;store final result into current element
    	mov ecx, loopcount		;restore ecx as a loop counter
    	inc ebx				;increment the array to next element
    
    	loop L1
    	ret
    
    ChinchillaCalculate ENDP
    
    
    
    
    
    
    
    
    DisplayTotal PROC
    
    	mov edx, OFFSET prompt2
    	call WriteString
    	mov eax, rate
    	call WriteInt
    	mov edx, OFFSET prompt3
    	call WriteString
    	mov eax, array[ebx]
    	call WriteInt
    
    	ret
    
    DisplayTotal ENDP
    
    END main

    For all of you who ever wanted to know the number of chinchillas you would have after 14 years.
    Last edited by The Brain; 09-21-2004 at 06:20 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Assembly
    By mrafcho001 in forum Tech Board
    Replies: 5
    Last Post: 03-12-2006, 05:00 PM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 03:51 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM