1 / 14

Control structures

Control structures. Hot to translate high level control structures to assembler. If a then b else c endif. Note that if a then b endif is a special case of the above. In PIC assembler this takes the general form: Assume a is a bit flag in a register

haamid
Download Presentation

Control structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control structures Hot to translate high level control structures to assembler

  2. If a then b else c endif • Note that if a then b endif is a special case of the above. • In PIC assembler this takes the general form: Assume a is a bit flag in a register • btfss a ; if a then skip next instr • goto l1 ; if a false goto l1 • ;code to do b • goto l2 • l1 • ;code to do c • l2

  3. while a do b endwhile • On a PIC you do the following • l1 ; start label • btfss a ; test condition a • goto l2 ; if a is false we go to end • ; code for action b • goto l1 ; try again • l2 ; end while label

  4. High Level Code Repeat A Until b; Assembler Startlabel ; code for A btfss b ; test b goto exitlabel goto startlabel exitlabel Repeat loop

  5. For Loops • For i:=20 downto 1 do A Movlw 20 ; set i:=20 Movwf regi Startlabel ; code for A DECFSZ regi,1 ; decrement skip zero goto startlabel ; repeat if >0

  6. Upward for loops • For i:=1 to 10 do A Movlw 10 ; set count:=10 Movwf count clrf regi ; initialise i to 0 Startlabel incf regi,1 ; increment I, thus =1 first time ; code for A DECFSZ count,1 ; decrement skip zero goto startlabel ; repeat if >0

  7. Switch statement • This is explained in the last lecture on state machines.

  8. A simple parameter-less subroutine can be done as follows Foo() Call foo Foo ; code for foo return Call subroutine

  9. Subroutines with parameters • In assembler one normally writes routines to pass parameters in registers, • If you are calling routines written in High level language from an assembler program on a big CISC processor like a Pentium, then you have to follow the high level language calling convention. • On RISC machines registers are used for parameters even on big processors

  10. Passing Parameters • Suppose we want to call a simple function foo which takes 2 byte integer parameters. Suppose that it just adds the two bytes for now. • We want the effect of calling • foo(1,2)

  11. Register passing of params • Suppose the C definition of foo was • foo(char a, char b){ return a+b;} • We may choose to pass parameter a in the W register and assign another register ( say 7f ) to pass B in.

  12. Example call code ; foo(1,2); MOVLW 02 ; w:=2 MOVWF 07Fh; reg7f:=w MOVLW 0x01 ; w:=1 CALL foo

  13. Called code ; char foo(char a, char b) ; { return (char)(a+b);} foo ADDWF 07fh,W RETURN • On entry a is in W and b is in reg7f, we return the result in W

  14. Nested calls • If foo were to call another routine then the w and 7f registers would be corrupted, thus you need to save the parameters in other registers at the start of foo foo MOVWF 24h ; save w in reg 24 MOVF 07Fh,W MOVWF 25h ; save reg7f in reg25 ; rest of code for foo

More Related