cosc 2150 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

cosc 2150

Description:

will jump to the address %rX and put the current address in %rY ... Because we do not have a stack for the emulator, you MUST store this value. ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 11
Provided by: csU64
Category:
Tags: cosc | emulator

less

Transcript and Presenter's Notes

Title: cosc 2150


1
cosc 2150
  • Subroutines in ARC

2
Assembly code
  • There are two instructions used for subroutine
    calls
  • call label
  • This will change the PC to where the labels
    address and put the current address into r15
  • jmpl rX4, rY
  • will jump to the address rX and put the current
    address in rY
  • Normally rX is r15, the 4 is because you would
    return the call instruction, instead of the next
    instruction.

3
example
  • . (instructions)
  • call sub_r ! call subroutine sub_r
  • sub r2, 2,r1
  • . (more instructions)
  • halt
  • sub_r addcc r1, 2, r2
  • jmpl r154, r0 !go back to sub instruction

4
Code style
  • Subroutines are written so that you execute a
    piece of code and then return back.
  • You can not branch out of a subroutine back to
    the "main" code.
  • You subroutines are not be "inside" the main code
    either.
  • The jmpl command, places the address of the
    instruction in r15. Because we do not have a
    stack for the emulator, you MUST store this
    value.
  • The reason will shown in example 2.

5
Example 1
  • main ()
  • int a 2
  • a a 3
  • sub_r()
  • void sub_r()
  • a a 1
  • .begin
  • .org 2048
  • ! main ld a,r1add r1,3, r1st r1,
    acall sub_r !call sub sub_rhalt
  • ! subroutine sub_r
  • sub_r st r15, sub_r_l
  • ld a,r1add r1,1,r1st r1, a
  • ld sub_r_l, r15jmpl r154,r5
  • sub_r_l 0
  • a2
  • .end

6
Example 2
  • main ()
  • int a2
  • sub1()
  • sub2()
  • sub1()
  • sub1 ()
  • a a 1
  • sub2()
  • sub2 ()
  • a a 2
  • .begin
  • .org 2048
  • ld a, r1 !load A into r1call sub1 !call
    sub sub1call sub2 !call sub sub2
  • call sub1 !call sub sub1halt
  • sub1 st r15, sub1_ladd r1, 1, r1st r1,
    acall sub2ld sub1_l, r15jmpl r154, r2
  • sub2 st r15, sub2_ladd r1, 2, r1st r1,
    ald sub2_l, r15jmpl r154, r2
  • sub2_l 0
  • sub1_l 0
  • a 2
  • .end

7
Recursion
  • Is recursion possible in ARC code?
  • No
  • A new subroutine would have to created for each
    instance of the recursion and ARC code does not
    have that possibility.
  • because we have store register 15, which would
    over write the old value.

8
Example 3
  • main ()
  • int a1, b 2
  • a a b
  • sub1()
  • b b a
  • sub1 ()
  • a b 1
  • b a 2
  • .begin
  • .org 2048
  • ! main ld a, r1ld b, r2add r1,r2,
    r1st r1, acall sub1 !call subroutine
    sub1add r1, r2, r2
  • st r2, b
  • halt
  • ! subroutine sub1
  • sub1 st r15, sub1_lsub r2,1,r1st r1,
    aadd r1, 2, r2st r2, bld sub1_l,
    r15jmpl r154,r5
  • sub1_l 0
  • a 1
  • b 2
  • .end

9
Example 4
  • main()
  • int a3, i
  • for (i0 ilt3 i)
  • if (i gt0)
  • sub1()
  • else
  • ai i 1
  • sub1 ()
  • ai i - 1
  • .begin
  • .org 2048
  • ld i, r1
  • top subcc r1, 3, r0 ! ilt3
  • bpos done
  • !if (igt0)
  • subcc r1, 1, r0
  • bneg else
  • call sub1
  • ba incr
  • else sll r1, 2, r2 !ai i -1
  • addcc r1, 1, r3
  • st r3, r2a
  • incr add r1, 1, r1 !i
  • ba top
  • st r1, i
  • done halt
  • sub1 st r15, sub1_l

10
Q
A
Write a Comment
User Comments (0)
About PowerShow.com