0% found this document useful (0 votes)
6 views

Prog Execution Diagram

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Prog Execution Diagram

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

105b4: 1101 addi sp,sp,-32 #105b4 <main> ->-- Main starts--

...... |
...... |
...... |
...... |
...... |
...... |
105d4: fa1ff0ef jal ra,10574 <add_5> |
105d8: 87aa mv a5,a0 #RA1 |
...... |
...... |
...... |
...... |
...... |
...... |
105f8: 8082 ret <---------Main ends here --------

//Note Main function start at 0x105b4 (Hex) and ends at 105f8 (Hex)
// Total main function is about (0x105f8-0x105b4) = (0x44+4) = 0x48 (hex) bytes
// Total of 0x48 bytes = 72 bytes
// If there is no JAL or any Jump, the PC starts at 0x105b4 continuosly
// increment to either 4 or 2 (depending 4-byte or 2byte instruction) and end
// at 0x105f8

//Simillary add_5 start at 0x10574 and ends at 0x105b2


//If there is no JAL or jump, the PC starts at 0x10574 and ends at 0x105b2
10574: 7179 addi sp,sp,-48 # 10574 <add_5> ->- add_5 function starts--
...... |
...... |
...... |
...... |
...... |
...... |
10596: f9fff0ef jal ra,10534 <add_4> |
1059a: 87aa mv a5,a0 # RA2 = 0x1059a |
...... |
...... |
...... |
...... |
...... |
...... |
105b2: 8082 ret <------add_5 function ends here ----------

//Simillary add_4 start at 0x10534 and ends at 0x10572


//If there is no JAL or jump, the PC starts at 0x10534 and ends at 0x10572
10534: 7179 addi sp,sp,-48 #10534 <add_4> ->-----add_4 starts --------
...... |
...... |
...... |
...... |
...... |
10556: fb3ff0ef jal ra,10508 <add_3> |
1055a: 87aa mv a5,a0 # RA3 = 0x1055a |
...... |
...... |
...... |
...... |
...... |
10572: 8082 ret <------add_4 function ends here -----------

//Simillary add_3 start at 0x10508 and ends at 0x10532


//Actuall no JAL or jump here in add_3 here in this case
//So,the PC starts at 0x10508 and ends at 0x10532
10508: 7179 addi sp,sp,-48 #10534 <add_4> ->-----add_3 starts --------
...... |
...... |
...... |
...... |
...... |
...... |
...... |
...... |
...... |
...... |
10532: 8082 ret <------add_3 function ends here -----------

How PC flow start goes

We will start discussion from main function 1st instruction i.e 0x105b4

PC=105b4: 1101 addi sp,sp,-32


....
....
// PC continues from 0x105b4 till jal instruction at 0x105d4
// after executing the JAL instruction at 0x10594 PC moves to 0x10574 (add_4)
// which is 1st instruction of add_4 function and Stores RA address
// i.e next instruction after jal at 0x105b4 which is "mv" instruction at
0x105d8
// This instruction address RA = 0x10598 is stored in the stack
// other wise, we will lose this RA address
// After JAL instruction 0x10594, PC moves to 0x1574 which 1st instruction of
add_5 function
105d4: fa1ff0ef jal ra,10574 <add_5> #RA = 105d8 - We call RA1
10574: 7179 addi sp,sp,-48 # 10574 <add_5>:
....
....
// Now PC continus from 0x10574 continus till the instruction jal at 0x10596
// PC moves to 0x10534 (add_4) which is 1st instruction of add_4 function
// and Stores RA address i.e next instruction after jal at 0x10596
// which is "mv" instruction at RA = 0x1059a (RA2)
// This instruction address RA = 0x1059a is stored in the stack otherwise we
will lose
// After JAL instruction at 0x10596, PC moves to 0x1574 which 1st instruction of
add_4 function
10596: f9fff0ef jal ra,10534 <add_4> #RA = 1059a -- We call
RA2
10534: 7179 addi sp,sp,-48 # 10534 <add_4>:
....
....
// Now PC continus from 0x10534 in add_4 till the instruction jal at 0x10556
// PC moves to 0x10508 (add_3) which is 1st instruction of add_3 function
// and Stores RA address i.e next instruction after jal at 0x1055a
// which is "mv" instruction at RA = 0x1055a (RA3)
// This instruction address RA = 0x1055a is stored in the stack otherwise we
will lose
// After JAL instruction at 0x10556, PC moves to 0x10508 which 1st instruction
of add_3 function
10556: fb3ff0ef jal ra,10508 <add_3> #RA = 1055a
10508: 7179 addi sp,sp,-48 # 10508 <add_3>:
.....
.....
// Now PC continus from 0x10508 in add_3 function till the instruction ret at
0x10532
//Note here in add_3 function does not have any JAL or Jump instruction
//It start 1st instruction of add_3 function to last instruction of the add_3
// function which is ret at 0x10532
// ret instrction retrives last entered RA address in the stack which is RA3 i.e
0x105aa
// Due to execution of ret instruction at 0x10532, PC moves to 0x105aa
// which is mv instrction in add_4 function
// After mv instruction, PC continous in add_4 function
10532: 8082 ret #1055a
1055a: 87aa mv a5,a0
....
....
....
//Now PC continus from 0x1055a in add_4 function till the instruction ret at
0x10572
// ret instrction retrives last entered RA address in the stack which is RA2 i.e
0x1059a
// Due to execution of ret instruction at 0x10572, PC moves to 0x1059a
// i.e mv instrction in add_5 function
// After mv instruction, PC continous in add_5 function
10572: 8082 ret #1059a
1059a: 87aa mv a5,a0
....
....
....
//Now PC continus from 0x1059a in the function add_5 till the instruction ret at
0x105d8
//ret instrction retrives last entered RA address in the stack which is RA1 i.e
0x105d8
//Due to execution of ret instruction at 0x1059a, PC moves to 0x105d8
//i.e mv instrction in main function
//After mv instruction, PC continous in main function
105b2: 8082 ret #105d8
105d8: 87aa mv a5,a0
....
....
....
//Now PC continus from 0x105d8 in the function main till the instruction ret at
0x105f8
//ret instrction retrives last entered RA address in the stack which we did not
discuss
//as we start the discussion from main_function only
105f8: 8082 ret #10494

Before First jal (stack is empty)

------------------
| |
------------------
| |
------------------
| |
------------------
| |
------------------

After First jal of main function at 0x105d4 (store RA1=0x105d8 after that stack has
one entry)

------------------
| |
------------------
| |
------------------
| |
------------------
| RA1=0x105d8 |
------------------

After Second jal in add_5 function at 0x10596 (store RA2=0x1059a after that stack
has two entries)

------------------
| |
------------------
| |
------------------
| RA2=0x1059a |
------------------
| RA1=0x105d8 |
------------------

After third jal in add_4 function at 0x10556 (store RA2=0x1055a after that stack
has two entries)

------------------
| |
------------------
| RA3=0x1055a |
------------------
| RA2=0x1059a |
------------------
| RA1=0x105d8 |
------------------

Note : add_3 does not have any jal instruction. It is start at


10508: 7179 addi sp,sp,-48 # 10508 <add_3>:
.....
continues till it sees ret instrutcion 10532: 8082 ret

After first ret at 0x10532 at add_3 function


(Retrive RA3 = 0x1055a after that stack has two entries)
Now PC moves 0x1055a which is in add_4 function

------------------
| |
------------------
| |
------------------
| RA2=0x1059a |
------------------
| RA1=0x105d8 |
------------------

PC continues from 0x1055a in add_4 till ret instruction in add_4 at 10572: 8082
ret
(Retrive RA2 = 0x1059a after that stack has one entry)
Now PC moves 0x1059a which is in add_5 function

------------------
| |
------------------
| |
------------------
| |
------------------
| RA1=0x105d8 |
------------------

PC continues from 0x1059a in add_5 till ret instruction in add_5 at 105b2: 8082
ret
(Retrive RA1 = 0x105d8 after that stack has zero entry)
Now PC moves 0x105d8 which is in main function

------------------
| |
------------------
| |
------------------
| |
------------------
| |
------------------

Now PC continues from 0x105d8 in main function till ret instruction at main
function
105f8: 8082 ret
We will stop our discussion at this point of time as we start 1st instruction of
main function

You might also like