CS273 - Machine Programming and Organization High-Level Language Constructs in Assembly Language In lab and in assignments, the students translate programs between high-level language and assembly language. In addition, the labs give guidance in in writing useful documentation. This document contains background material about high-level language constructs and assembly language. See the labs and assignments for more examples of translations of high-level language constructs into assembly language code. ================================================================== The following assembler directive can be used to allocate an array, called ArrayA, of ten one-byte elements. ArrayA rmb 10 Here is an example of some high-level language code to initialize the array so that ArrayA(i) = i, i=0,...,N-1: i = 0; do { ArrayA[i] = i; i = i+1; } while (i < N); This segment could be translated into the following 6811 assembly langauge program. N EQU 6 org $0 data ArrayA rmb N A(i), i=0, ...,N-1 org $f800 instructions start clra * i = 0 ldx #ArrayA * point at first element of array loop: * do { staa 0,x * A(i) = i inx * point at next array element inca * i++ cmpa #N blo loop * } while (i < N) end jmp end array initialization complete org $fffe initialize reset interrupt vector fdb start to beginning of program ----------------------------------------------------------------------------- Consider the following assembly language program segment: label1 ldaa arg1 cmpa arg2 bgt continue inca continue staa temp This can be expressed in a high-level language as follows. if (arg1 <= arg2) then temp = arg1+1 else temp = arg1 endif The instructions of a high-level program make excellent comments for an assembly language program. If we use this high-level programming language construct to comment and label the assembly language program segment, it might look like this: if ldaa arg1 cmpa arg2 if (arg1 <= arg2) bgt endif inca then temp = arg1+1 endif staa temp else temp = arg1 The following are general high-level programming language constructs. if (condition) then actions; if (condition) then actions1; else actions2; if (condition) then actions else if (condition) then actions; ... else actions; for (initial condition; continuing condition; increment) { actions} while (condition) { actions; } for (initial condition; continuing condition; increment) { actions; } switch (variable) { case 1: actions; break; case 2: actions; break; case 3: case 4: actions; break; default: actions; break; } In the above, ``actions'' is either a single action or a compound action. ================================================================ Below are templates and examples of high-level language constructs in assembly language. The high-level constructs are written in pseudo code, rather than C. 1. IF OP1 REL OP2 THEN < BODY > ENDIF IF: LDAx OP1 CMPx OP2 B(NOT REL) ENDIF < BODY> ENDIF: EXAMPLE: IF [MemA] LE[MemB] THEN < Increment REG B> ENDIF IF: LDAA MemA CMPA MemB BGT ENDIF INCB ENDIF: 2. IF OP1 REL OP2 THEN < BODY1 > ELSE < BODY2 > ENDIF IF: LDAx OP1 CMPx OP2 B(NOT REL) ELSE < BODY1> BRA ENDIF ELSE: < BODY2> ENDIF: EXAMPLE: IF MemA < 18 THEN < Decrement Reg B > ELSE < Increment Reg B> ENDIF IF: LDAA MemA CMPA #18 BGE ELSE DECB BRA ENDIF ELSE: INCB ENDIF: 3. WHILE OP1 REL OP2 DO < BODY > ENDWHILE WHILE: LDAx OP1 CMPx OP2 B(NOT REL) ENDWHILE < BODY> BRA WHILE ENDWHILE: EXAMPLE: WHILE MemA < UB THEN < Shift Left Reg A> ENDWHILE WHILE: LDAB MemA CMPB UB BGE ENDWHILE LSLA BRA WHILE ENDWHILE: 4. DO LCV = START, STOP, INCREMENT < Body >ENDDO DO_INIT: LDAx START STAx LCV DO: LDAx LCV CMPx STOP BGT ENDDO < BODY1> LDAx LCV ADDx INCREMENT STAx LCV BRA DO ENDDO EXAMPLE: DO [I] = START, STOP, INCREMENT< Sum=Sum+1 > ENDDO DO_INIT: LDAA START STAA I DO: LDAA LCV CMPA STOP BGT ENDDO INC SUM LDAA LCV ADDA INCREMENT STAA LCV BRA DO ENDDO 5. switch (VAR) ... CASE: LDAx VAR CMPA OPTION1 BNE OPT2 ........option1 code BRA ENDCASE OPT2: CMPA OPTION2 BNE OPT3 ........option2 code BRA ENDCASE OPT3: CMPA OPTION3 BNE OPT4 ........option3 code BRA ENDCASE OPTX: CMPA OPTIONX BNE DEFAULT ........optionx code BRA ENDCASE DEFAULT: ........default case code ENDCASE: EXAMPLE: CASE INPUT OF [R,W,S,T] ENDCASE CASE: LDAA INPUT CMPA #'R BNE CMD_W ........ BRA ENDCASE CMD_W: CMPA #'W BNE CMD_S ........ BRA ENDCASE CMD_S: CMPA #'S BNE CMD_T ........ BRA ENDCASE CMD_T: CMPA #'T BNE DEFAULT ........ BRA ENDCASE DEFAULT: ........ ENDCASE: 6. REPEAT < Body >UNTIL OP1 REL OP2 REPEAT: < Body> UNTIL: LDAx OP1 CMPx OP2 BREL REPEAT ENDREPEAT: EXAMPLE: REPEAT < Sum=Sum+1 > UNTIL INPUT GT MAX REPEAT: INC SUM UNTIL: LDAA INPUT CMPA MAX BLE REPEAT ENDREPEAT: