-1

I need to write some Lines in 68k Assembly Language with the math formula:

x^2-5x+6

I want to do it with ADD and SUB commands and MOVE yet somehow I cant define the variable x it says its an undefined Symbol and I cant actually realize where my problem is.

ORG    $1000
START:                  ; first instruction of program

MOVE    X*X, D0
MOVE    (-5X),D2  
MOVE    6,D3 
ADD     D0, D3
SUB     D2, D1



SIMHALT

Errors: LINE 10 Invalid Syntax LINE 11 Invalid Syntax

HaskellPlease
  • 277
  • 2
  • 10

2 Answers2

2

Something like this, assuming basic 68000 (and not 68020 or better).

You may have to fix matters like whether X is a word or long word and deal with matters such as sign extension as its a long time since I did 68k assembler. X is defined as a word constant at the end.

ORG    $1000
START:                  ; first instruction of program
    CLR.L  D7        ; Clear D0 - alternatively MOVEQ #0,D0
    MOVE.W X,D7      ; Read X

    ; Output initial value...
    LEA    S1,A1
    MOVE.W #255,D1
    MOVE.L D7,D1
    MOVEQ  #17,D0
    TRAP   #15

    LEA    SNUL,A1
    MOVEQ  #13,D0
    TRAP   #15

    MOVE.L D7,D6     ; copy of X
    ASL.L  #2,D6     ; Multiply by 4
    ADD.L  D7,D6     ; 4X plus another X = 5X
    MULU.W D7,D7     ; X^2  
    SUB.L  D6,D7     ; Subtract 5X from X^2
    ADDQ.L #6,D7     ; plus 6

    ; Output answer...
    LEA    S2,A1
    MOVE.L D7,D1
    MOVEQ  #17,D0
    TRAP   #15

    SIMHALT             ; halt simulator

* Put variables and constants here
S1:   DC.B 'Initial :',0
S2:   DC.B 'Answer  :',0
SNUL: DC.B 0
X:     DC.W 1234    ; Initial (fixed) value of X

END    START        ; last line of source
vogomatix
  • 4,856
  • 2
  • 23
  • 46
1

Declaring variables in assembly doesn't work like it would in C or other similar languages. Let's say you're trying to write the following C function:

int myFunction(int x)
{
   return (x**2) + (-5x) + 6;
}

So what you would do is, you would choose a register, say D0, and let that be your input variable. It can also be where the output goes.

myFunction:
MOVE.L D0,D1
MULS D0,D1    ;D1 = x squared
MOVE.L D0,D2  
ADD.L D0,D0
ADD.L D0,D0
ADD.L D2,D0   ;D0 = 5X

SUB.L D0,D1   ;D1 = (X^2) - 5X
ADD.L #6,D1   ;D1 = (X^2) - 5X + 6
MOVE.L D1,D0  ;return in D0
RTS         

Now, if you wanted to use this function, you would first load the desired value of x into register D0 and then call the function:

MOVE.L #5,D0  ;as an example, calculate the function where x = 5.
JSR myFunction
;the program will resume here after the calculation is done, 
;and the result will be in D0.
puppydrum64
  • 1,598
  • 2
  • 15