This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430/Assembly Calculations

Hi,

I am trying to perform the calculation: 

y = (-7a+15)^2

where a is either a positive or negative integer.

I'm having problems with moving -7 into a register in the first place, it keeps showing up as 0x00F9 = 249.
I don't know if this is correct or not because our Professor informed us to simply enter a negative integer as such: #-7.
The result when a=10 should be 3025. I am not getting that result. 

Any help is appreciated. Thank you.





Code:

YCALC		mov.b	R4,  R11				;use  "a" as counter
			mov.b	R4,  R12				;move multiplicand "a" into R12
			mov.b	#-7, R13				;move multiplier into R13
			jmp COMPARE						;jump to compare
;-------------------------------------------------------------------------------
COMPARE		cmp #0, R4	 					;compare multiplcand to 0
			jl  YNEG						;if multiplicand negative, go to negative mult loop
			jmp YPOS						;if multiplicand positive, go to positive mult loop

YNEG		cmp #0, R11						;compare 0 to counter
			jeq STORE						;if multiplcation finished, go to STORE
			add.w 	R12, R13				;add R12, R13
			inc R11							;increase counter
jmp YNEG
YPOS cmp #0, R11 ;compare 0 to counter jeq STORE ;if multiplcation finished, go to STORE sub.w R12, R13 ;subtract R12, R13 dec R11 ;decrease counter
jmp YPOS STORE add.w #15, R13 ;add #15 to multiplcation result add.w R13, R13 ;perform squaring operation mov.w R13, R6 ;move final result into R6 jmp FCALC ;go to FCALC



  • Ben Trigoboff said:
    I'm having problems with moving -7 into a register in the first place, it keeps showing up as 0x00F9 = 249. I don't know if this is correct or not because our Professor informed us to simply enter a negative integer as such: #-7.

    That's correct, it's due to the fact that negative values are stored in two's complement format on MSP430 (and many other CPUs too). 

    EDIT: I think this page does a better job of explaining negative number representation than the main two's complement page: http://en.wikipedia.org/wiki/Signed_number_representations

  • >moving -7 into a register in the first place, it keeps showing up as 0x00F9 = 249

    That is correct, if you use signed instructions on these numbers everything works out correct in the end.
    signed 8bit numbers are from -128 to 127,
    But if you look at them in a purely 8bit representation they will of course be in the range of 0-255
     
    Mixing signed 8bit in to signed 16bit result can you have to make sure that you do things correct.

    Comparison of signed integers:
    JGE: Jump if greater or equal
    JL: Jump if less
    JN: Jump if negative

    SXT: Extend Sign
    RRA: Rotate right arithmetically
    RLA: Rotate left arithmetically

    The first part should result in: -55 (0xC9 in memory)

    (-55)*(-55)= 3025

    This is because to square a number just means to multiply it by itself.
    For example, (-2) squared is (-2)(-2) = 4.
    This is positive because when you multiply two negative numbers you get a positive result.

    >add.w   R13, R13                ;perform squaring operation
    looks more like multiply by 2 to me

  • Thank you.

    Yes, I'm only adding the result to itself right now, I didn't even make it that far to perform the squaring. In order to square the result, would you suggest I create another loop and add 55 to itself 55 times, or is there a more efficient algorithm in assembly that I am unaware of?
  • Ben Trigoboff said:
    create another loop and add 55 to itself 55 times, or is there a more efficient algorithm in assembly that I am unaware of?

     At first is this MSP430 related? I think no.

     To do multiplication which device are you planning to use? Has this multiplication hardware or not? Can move multiply terms to hardware register then read the result or call a software multiply, if you need do as exercise I think basic shift and add is better than add loop.. Programming zen teach not to do algorithm increasing time in exponential way or time dependent on value of operand.

  • My old post where I do 8bit *8bit in assembly pretty fast

    e2e.ti.com/.../340445

    It don't work for signed bytes, but as both are negative just prepare data:
    inv.b R13 
    inc.b R13
    mov.b R13.,R12
    call #multiply

  • I'm using the MSP430... the chip is the G2_2553, the language is assembly.
  • software multiplication is a general term and is not very specific to the MSP430, so that is why he recommend to google it.

  • Looks good, but slightly beyond what I'm familiar to, as I don't quite understand what rotate through carry does, what a swap bytes is accomplishing or the the jump if no carry is doing. Still, I greatly appreciate the assistance and will run your code to try to figure out what it's doing.
    The class I'm taking that's suited for electrical engineers doesn't go nearly as in depth as the same course that's tailored for computer science majors... unfortunately in the engineers section, we haven't been exposed to the same material as the computer science section, but we are expected to complete the same labs... without the same knowledge. Good learning experience though as our whole class is basically researching and utilizing alternate resources to figure these things out.
  • >Looks good, but slightly beyond what I'm familiar to, as I don't quite understand what rotate through carry does

    http://www.physics.mcmaster.ca/phys3b06/MSP430/Instruction_Set.pdf

    Swap switches bytes in a words, also useful for a shortcut for: byte*256=word or byte<<8

    This maybe make it little easier to understand what it does (Parallax assembler)
    it's using lower 8bit shifted out in R13 to know when to add R12 to upper bits of R13,
    but as R13 is the same register that is shr, its true value in the end emerge.

                    shl     R12, #7        ' prepare R12 so it adds to the upper bits of R13 
                    shr     R13, #1 wc     ' bit 0
            if_c    add     R13, R12       ' add R12 to > R13 (in pasm source is last)
                    shr     R13, #1 wc     ' bit 1
            if_c    add     R13, R12
                    shr     R13, #1 wc     ' bit 2
            if_c    add     R13, R12
                    shr     R13, #1 wc     ' bit 3
            if_c    add     R13, R12
  • Ben Trigoboff said:
    . without the same knowledge. Good learning experience though as our whole class is basically researching and utilizing alternate resources to figure these things out.

     That's called "home work" so I am pay'd to do lesson to my pupils not to give free lesson, sorry this is MSP430 related forum and if something is relative to specific problem welcome here, if this has to switch on a free lesson to somewhere professor take money and we lose .. this is not a good idea for us we are here to share experience not to teaching job, none here pay us.

     Rotate thru carry are for extending arithmetic operation over multi byte/word sizes and all processor work same way from era of electromechanics to mainframe and today computer...

    http://www.computerhistory.org/fellowawards/hall/bios/Konrad,Zuse/

     please tell your professor and your colleagues university has to teach and student have to learn about here we don't solve troubles of and we can do limited support. Sucking this resource shorten future of technology and industry leaving unprepared people to real world.

**Attention** This is a public forum