60 likes | 204 Views
Algorithms to Assembly. How to translate pseudo code to assembly language programs. MSP430 Program Flow Instructions. Case Unsigned Signed. A eq B jeq jeq A neq B jne jne A >= B jhs jge A < B jlo jl.
E N D
Algorithms to Assembly How to translate pseudo code to assembly language programs
MSP430 Program Flow Instructions Case Unsigned Signed A eq B jeqjeq A neq B jnejne A >= B jhsjge A < B jlojl Note there are no “jls” or “jle” instructions, so must use a compound Statement such as “jle” => jl OR jeq
Conditinal Jump and Control Structures Start with an algorithm in pseudocode/Java/C: unsigned integer If ( a >= b ) { a = a + b } else { a = a -b } 1. Resource allocation Assign variables to registers 2. Convert to Register Transfer Notation Swap out variables with registers Change “=“ to “<-” a => r5 b => r6 if( r5 >= r6 ){ r5 <- r5 + r6 } else { r5 <- r5 – r6 } 3. RTN to Assembly Match forms of Algothm->Assembly Insert Opcodes. cmp r5,r6 jhsNotLessThan sub r5,r6 jmp End NotLessThan: add r5,r6 End:
Conditinal Jump and Control Structures Usually an “if” statement implies a “cmp” RTN to Assembly: The If/Then Statement cmp r5,r6 jhsNotLessThan NotLessThan: add r5,r6 if ( r5 >= r6 ) { r5 <- r5 + r6 } else { r5 <- r5 –r6 } sub r5,r6 jmp End End: The “else” part does not require a separate jump. If/then statements cause jmpsforward
Conditinal Jump and Control Structures Usually a “while” statement implies a “cmp” RTN to Assembly: The While Statement Part 1 “while” loops cause jmp backwards while ( n > 0 ) { s1… } Loop: s1… dec r6 cmp #0,r6 jnz Loop End: Until condiion is met, then Jmp forward.
RTN to Assembly: The While Statement Part 2 Usually a “while” statement implies a “cmp” “while” loops cause jmp backwards while ( n > 0 ) { s1… } Loop: cmp #0,r6 dec r6 jz End s1… jmp Loop End: Until condiion is met, then Jmp forward.