Shop OBEX P1 Docs P2 Docs Learn Events
mov M,#lit and best way of IF...THEN in SASM — Parallax Forums

mov M,#lit and best way of IF...THEN in SASM

Mag748Mag748 Posts: 269
edited 2005-01-09 03:45 in General Discussion
Two questions here:
1) In SASM the instruction mov M,#lit will mov the # into the M register's lower 4 bits (SX28). But in the Example in the SxUsersManual, it says "mov M,#lit controls the four lower bits of the MODE register. The two subsequent “mov M,#lit” instructions change the lower four bits of the MODE register."

What is the difference between using mov M,#lit and mov W,#lit then mov M,W?



2) What is the best method of doing an IF...THEN condition in SASM?

I want to say:

IF RB.0 =1 AND RB.1 = 1 THEN jmp....

I need to use jb, and/or jnb, right?



Thanks, Marcus


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


Post Edited (Mag748) : 1/7/2005 12:47:23 PM GMT

Comments

  • pjvpjv Posts: 1,903
    edited 2005-01-07 05:12
    Hello Marcus;

    In the case of the SX28 there is no difference between MOV M,W and MOV W,#lit then MOV M,W.

    However, in the SX48/52 this is not the case as these use the lower 5 bits, and the MOV M,W instruction only deals with the lower 4 bits.

    As to your other question I'm not sure.

    Peter
  • BeanBean Posts: 8,129
    edited 2005-01-07 12:47
    I haven't tried it, but I would expect this to work:

    MOV W,RB ; Get bits
    AND W,#3 ; Isolate bits
    XOR W,#3 ; Invert bits
    JZ label ; Jump if inverted bits were both zero

    bean.
  • Mag748Mag748 Posts: 269
    edited 2005-01-07 12:57
    pjv said...

    In the case of the SX28 there is no difference between MOV M,W and MOV W,#lit then MOV M,W.

    That's true, but is there any difference between mov M,#lit and mov W,#lit THEN mov M,W

    Bean, thank you, I will try that.

    Thanks, Marcus

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • DaveGDaveG Posts: 84
    edited 2005-01-07 16:16
    Marcus,

    In the SX28 there are only 8 modes, so 4 bits are enough. The Mode instruction (MODE literal) and Mov M,#lit only
    move the lower 4 bits of a byte into the Mode register. The upper bits are ignored.

    Now in the case of the SX48/52, there are many more modes and 5 bits are required. However, If you used the
    MODE literal instruction as you did with the SX28, only 4 bits would be transferred, the 5th bit would be lost.
    Therefore, you must use Mov W,#lit then Mov M,W to move all eight bits into the Mode register. Although the
    upper 3 bits are ignored.

    With SX28:
    Use MODE Literal
    Or Mov M, #lit
    Or Mov W,#lit followed by Mov M,W

    With SX48/52:
    Use Mov W,#lit followed by Mov M,W

    Dave G
  • Mag748Mag748 Posts: 269
    edited 2005-01-07 16:41
    Thank you. I understand about the Mode stuff now, thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • James NewtonJames Newton Posts: 329
    edited 2005-01-07 23:37
    Mag748 said...

    2) What is the best method of doing an IF...THEN condition in SASM?

    I want to say:

    IF RB.0 =1 AND RB.1 = 1 THEN jmp....

    I need to use jb, and/or jnb, right?
    snb rb.0
    sb rb.1
    skip
    jmp ...

    If you stare at that for a while it will make sense... probably. It still confuses the heck out of me. This is the area where SX/B, SX/C, etc... really make a difference. In fact, it would be very interesting to see what they compile for the above line. But no time right now... I have to get back to the newsletter I'm writing on how to master SASM macros...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james@sxlist.com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-01-07 23:55
    In re: your IF THEN question, if you are testing two bits of seperate registers then the jb,jnb combo is likely the best approach. This is a "top of my head" answer, so there may be a faster solution (James illustrates one). But in your particular example I know a shortcut, your example is testing the contents of the two bits of the same register (determining if rb= 'XXXX XX11") this can be done with the following code:

    mov w, rb
    and w, #$03 ;mask the bits we are intersted in (2 lowest)
    xor w, #$03 ;this is a backhanded equality operation
    jnz :false ; if result is not zero then last two bits are not 1
    ;code for true condition
    :false ....

    explanation in a nutshell: if you take your test variable and XOR it with the value you are looking for, the result will be 0 if and only if the test variable is equal to the value ($03 xor $03 = $00, $02 xor $03 = $01, $00 xor $03 = $03 etc) you can use this technique for any number of bits in the same register and comparing it with any value. If you have "don't care" bits in the variable your testing (bits you are not testing) mask the test variable before XORing it by ANDing the·variable with a value where a binary 1 for that bit means we're testing it, 0 means we're not.

    Hope this helps,
    Paul

    PS James' algorithm and mine take the same number of program words and take the same amount of time (4 cycles for a false, 6 cycles for a true), if you're testing 3 or more bits, my algorithm will be faster and occupy less code space (since mine always consumes 4 program words and takes 4/6 cycles regardless of the number of bits (well testing 8 bits drops it to 3 program words and·3/5 cycles·since you don't need the·AND but thats not the point)).

    Post Edited (Paul Baker) : 1/8/2005 12:32:00 AM GMT
  • James NewtonJames Newton Posts: 329
    edited 2005-01-08 00:41
    Paul Baker said...

    PS James' algorithm and mine take the same number of program words and take the same amount of time (4 cycles for a false, 6 cycles for a true), if you're testing 3 or more bits, my algorithm will be faster and occupy less code space (since mine always consumes 4 program words and takes 4/6 cycles regardless of the number of bits (well testing 8 bits drops it to 3 program words and·3/5 cycles·since you don't need the·AND but thats not the point)).
    Not that I care, but isn't jnz actually compiled as snz, jmp? In which case yours is 5/7 rather than 4/6.

    Also, the skip, jump only version preserves W and the status register.

    Also, it is not limited to bits in the same byte.

    For 3 or more bits, the and xor jmp is probably the only reasonable answer.

    This is a classic case where macros can be written to use conditional assembly and always compile the best code without requiring the programmer to remember all this junk. e.g.

    testbits macro
    IF \0·=·2
    · snb \1
    · sb \2
    · skip
    · jmp
    · ENDIF
    IF \0 > 2
    · REPT

    and so on...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james@sxlist.com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-01-09 03:45
    You are correct, I stand corrected,·it was·a back of a napkin calculation. Your algorithm was posted while I was writing mine, so I edited mine quickly.
Sign In or Register to comment.