SlideShare a Scribd company logo
1 of 28
CEG 320/520: Computer Organization and The Stack and
The Stack and Subroutines
CEG 320/520: Computer Organization and The Stack and
The Stack: Introduction
• A7 is a special address register, called the stack
pointer.
• When programming assembly, we can use SP as an
alias for A7. MOVEA.L #$3000,SP
• In the simulator, it is also called US (user stack
pointer)
• There is also a supervisor stack pointer, but we won’t
worry about it yet.
• Since our program usually starts at a low memory
address and grows upward, we start the stack at a
high memory address and work downward.
CEG 320/520: Computer Organization and The Stack and
The Stack: Introduction
• We push values onto the stack using
predecrement mode
– MOVE.W D2,-(SP)
– MOVE.W D3,-(SP)
• We pop values from the stack using
postincrement mode
– MOVE.W (SP)+, D3
– MOVE.W (SP)+, D2
• Some instructions affect the stack directly
CEG 320/520: Computer Organization and The Stack and
The Stack: Operations (push/pop)
$7000
$6FFE
$6FFC
main MOVE.W #12,-(A7)
MOVE.W #20,-(A7)
MOVE.W #30,-(A7)
…
MOVE.W (A7)+,D0
MOVE.W (A7)+,D1
MOVE.W (A7)+,D2
PUSH(12) Last-in, first-out
PUSH(20) (LIFO)
PUSH(30)
POP = 30
POP = 20
POP = 12
CEG 320/520: Computer Organization and The Stack and
The Stack: Purposes
• Temporary storage of variables
• Temporary storage of program addresses
• Communication with subroutines
– Push variables on stack
– Jump to subroutine
– Clean stack
– Return
CEG 320/520: Computer Organization and The Stack and
Programming Subroutines
• Why use subroutines?
– Code re-use
– Easier to understand code (readability)
– Divide and conquer
• Complex tasks are easier when broken down into
smaller tasks
• How do we call a subroutine in assembly?
– Place the parameters somewhere known
– JSR or BSR to jump to the subroutine
– RTS to return
CEG 320/520: Computer Organization and The Stack and
C Assembly
main MOVE.W A,D1
JSR sqr
MOVE.W D0,B
move.w #228,D7
TRAP #14
;*** subroutine sqr ***
sqr MUL.W D1,D1
MOVE.W D1,D0
RTS
ORIGIN $2000
A DC.W 5
B DS.W 1
end
main() {
int a, b;
a = 5;
b = sqr(a);
printf(“%dn” b);
}
/* subrtn sqr */
int sqr(int val) {
int sqval;
sqval = val * val;
return sqval;
}
CEG 320/520: Computer Organization and The Stack and
JSR and BSR
• JSR label
– MOVE.L PC, –(SP)
– LEA address-of-label, PC
In other words:
– SP ← [SP] – 4
– [SP] ← [PC]
– PC ← address-of-label
• BSR label
– Same, but offset from the current PC is stored
instead of the absolute address
CEG 320/520: Computer Organization and The Stack and
JSR example
1000 MOVE.L #5,D1
1006 LEA ARRAY,A0
100C JSR SUMARR
1012 MOVE.L D0,SUM
1018 MOVE.L #228, D7
101E TRAP #14
1020 SUMARR CLR.L D0
…
RTS
ARRAY DC.L 12,15,31
SUM DS.L 1
END
$7000
00001012PC
?
1012
0000
$6FFE
$6FFC
00007000A7
CEG 320/520: Computer Organization and The Stack and
RTS
• RTS
• Pop the address from the stack back into the
PC
– MOVE.L (SP)+, PC
In other words:
– PC ← [SP]
– [SP] ← [SP] + 4
CEG 320/520: Computer Organization and The Stack and
RTS example
1000 MOVE.L #5,D1
1006 LEA ARRAY,A0
100C JSR SUMARR
1012 MOVE.L D0,SUM
1018 MOVE.L #228, D7
101E TRAP #14
1020 SUMARR CLR.L D0
…
1032 RTS
ARRAY DC.L 12,15,31
SUM DS.L 1
END
$7000 ?
1012
0000
$6FFE
$6FFC
00001034PC
00006FFCA7
CEG 320/520: Computer Organization and The Stack and
Passing parameters in registers
main MOVE.W A,D1
JSR sqr
MOVE.W D0,B
move.w #228,D7
TRAP #14
;
sqr MOVE.W D1,D0
MULS.W D0,D0
RTS
ORIGIN $2000
A DC.W 5
B DS.W 1
end
• The number to be
squared is in D1.
• The result is
returned in D0,
D1 is unchanged.
CEG 320/520: Computer Organization and The Stack and
Parameter passing on the stack
• If we use registers to pass our parameters:
– Limit of 7 parameters to/from any subroutine.
– We use up registers so they are not available to our
program.
• So, instead we push the parameters onto the
stack.
• Our conventions:
– Parameters are passed on the stack
– One return value can be provided in D0.
– D0, D1, A0, A1 can be used by a subroutine.
Other registers must first be saved.
CEG 320/520: Computer Organization and The Stack and
First things first…
• Both the subroutine and the main program
must know how many parameters are being
passed!
– In C we would use a prototype:
int power (int number, int exponent);
• In assembly, you must take care of this
yourself.
• After you return from a subroutine, you must
also clear the stack. You have to clean up
your mess.
CEG 320/520: Computer Organization and The Stack and
Passing parameters on the stack
Mul3 – multiply three numbers and place the result in D0.Mul3 – multiply three numbers and place the result in D0.
; ******** Main Program ********************************
1000 START MOVE.W NUM1,-(SP) ;Push first param
1006 MOVE.W NUM2,-(SP) ;Push 2nd
param
100C MOVE.W NUM3,-(SP) ;Push 3rd
param
1012 JSR MUL3
1018 ADDA.L #6,SP ;Clean the stack!
101E MOVE.W #228,D7
1020 TRAP #14
; ******* Subroutine Mul3 ******************************
1022 MUL3 MOVE.W 4(SP),D0 ;D0 = NUM3
1026 MULS.W 6(SP),D0 ;D0 *= NUM2
102A MULS.W 8(SP),D0 ;D0 *= NUM1
102E RTS ;SP --> rtrn addr!
ORG $2000
2000 NUM1 DC.W 5
2002 NUM2 DC.W 8
2004 NUM3 DC.W 2
END
CEG 320/520: Computer Organization and The Stack and
Know how to…
• Push parameters onto the stack
• Access parameters on the stack using indexed
addressing mode
• Draw the stack to keep track of subroutine
execution
– Parameters
– Return address
• Clean the stack after a subroutine call
CEG 320/520: Computer Organization and The Stack and
Review problem
; ******** Main Program *********
1000 START MOVE.L NUM1,-(SP)
1006 MOVE.L NUM2,-(SP)
100C MOVE.L NUM3,-(SP)
1012 JSR SUB1
1018 Next instr…
… …
; ******* Subroutine SUB1 *******
1022 SUB1 …
1026 …
102E RTS
ORG $2000
2000 NUM1 DC.L $150
2004 NUM2 DC.L $180
2008 NUM3 DC.L $12
END
CEG 320/520: Computer Organization and The Stack and
Writing transparent subroutines
• A transparent subroutine doesn’t change any
registers except D0, D1, A0 and A1.
• If we need more registers than this, we must
save the register values when we enter the
subroutine and restore them later.
• Where do we store them? You guessed it: the
stack.
• The 68000 provides a convenient instruction,
MOVEM, to push the contents of several
registers to the stack at one time.
CEG 320/520: Computer Organization and The Stack and
A transparent subroutine
subr1 MOVEM.L D2-D3/A2-A3,-(SP)
MOVE.L #32,D2
MOVE.L 20(SP),D3
…
MOVEM.L (SP)+,D2-D3/A2-A3
RTS
$7000 ?
rtrn
rtrn
$6FFE
$6FFC
D2
D2
D3
D3
A2
A2
A3
A3
P1
P2
We can now safely modify D2, D3,We can now safely modify D2, D3,
A2 and A3, knowing that we willA2 and A3, knowing that we will
restore their original contents later.restore their original contents later.
We saved 4 registers, so the lastWe saved 4 registers, so the last
parameter lives at SP + (4parameter lives at SP + (4×4) + 4.×4) + 4.
(4 bytes/reg + 4 for the return addr.)(4 bytes/reg + 4 for the return addr.)
CEG 320/520: Computer Organization and The Stack and
Review problem #2
; ******** Main Program *********
1000 START MOVE.L NUM1,-(SP)
1006 MOVE.L NUM2,-(SP)
100C MOVE.L NUM3,-(SP)
1012 JSR SUB1
1018 Next instr…
… …
; ******* Subroutine SUB1 *******
1022 SUB1 MOVEM.L D2-D3/A4,-(SP)
1026 … ; show stack
102E MOVEM.L (SP)+,D2-D3/A4
1032 RTS
ORG $2000
2000 NUM1 DC.L $150
2004 NUM2 DC.L $180
2008 NUM3 DC.L $12
END
CEG 320/520: Computer Organization and The Stack and
Know how to…
• Write transparent subroutines
• Draw the stack and access parameters on the
stack, taking into account the saved register
values and the subroutine return address.
CEG 320/520: Computer Organization and The Stack and
Passing by value & reference
• We pushed the value of NUM1, NUM2, and
NUM3 on the stack.
• What if we want to change the input parameter
values?
• For example, what if we want to write a
subroutine that will multiply all of its
arguments’ values by 2, actually changing the
values in memory?
• We must pass the parameters by reference…
CEG 320/520: Computer Organization and The Stack and
The PEA instruction
• PEA – Push effective address
PEA label
is the same as…
LEA label,A0
MOVEA.L A0,-(SP)
but without using A0.
CEG 320/520: Computer Organization and The Stack and
Passing parameters by reference
dbl3 – double the values of three parametersdbl3 – double the values of three parameters
; ******** Main Program *********
1000 START PEA NUM1
1006 PEA NUM2
100C PEA NUM3
1012 JSR dbl3
1018 ADDA.L #12,SP
101E MOVE.W #228,D7
1020 TRAP #14
ORG $2000
2000 NUM1 DC.W 5
2002 NUM2 DC.W 8
2004 NUM3 DC.W 2
END
$7000 ?
2002
0000
$6FFE
$6FFC
2004
0000
1018
0000
2000
0000
CEG 320/520: Computer Organization and The Stack and
Using parameters passed by reference
dbl3 – double the values of three parametersdbl3 – double the values of three parameters
; ******* Subroutine dbl3 *********
DBL3 MOVEA.L 4(SP),A0
MOVE.W (A0),D0
MULS.W #2,D0
MOVE.W D0,(A0)
MOVEA.L 8(SP),A0
… ; repeat for each
RTS
ORG $2000
2000 NUM1 DC.W 5
2002 NUM2 DC.W 8
2004 NUM3 DC.W 2
END
?
2002
0000
2004
0000
1018
0000
2000
0000
$7000
$6FFE
$6FFC
CEG 320/520: Computer Organization and The Stack and
Putting it all together
MAIN MOVE.W COUNT,-(SP)
PEA ARRAY
JSR SUB2
…
SUB2 MOVEM.L D2-D3,-(SP)
;HERE
ORG $2000
COUNT DC.W 105
ARRAY DC.W 10,12,3,7,9,18
• What does the stack look like at the line
;HERE ?
CEG 320/520: Computer Organization and The Stack and
Characteristics of good subroutines
• Generality – can be called with any arguments
– Passing arguments on the stack does this.
• Transparency – you have to leave the registers like
you found them, except for D0, D1, A0, and A1.
– We use the MOVEM instruction for this purpose.
• Readability – well documented.
– See the example handout.
• Re-entrant – subroutine can call itself if necessary
– This is done using stack frames, something we will look at
in a few days…
CEG 320/520: Computer Organization and The Stack and
Know how to…
• Pass parameters by value and by reference
• Use address registers to access parameters
passed by reference
• Write general, transparent, readable
subroutines

More Related Content

What's hot

Chasing the optimizer
Chasing the optimizerChasing the optimizer
Chasing the optimizerMauro Pagano
 
8086 microprocessor instruction set by Er. Swapnil Kaware
8086 microprocessor instruction set by Er. Swapnil Kaware8086 microprocessor instruction set by Er. Swapnil Kaware
8086 microprocessor instruction set by Er. Swapnil KawareProf. Swapnil V. Kaware
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c eraMauro Pagano
 
Microprocessor 8086 instructions
Microprocessor 8086 instructionsMicroprocessor 8086 instructions
Microprocessor 8086 instructionsRavi Anand
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086aviban
 
MariaDB 10.0 Query Optimizer
MariaDB 10.0 Query OptimizerMariaDB 10.0 Query Optimizer
MariaDB 10.0 Query OptimizerSergey Petrunya
 
A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013Connor McDonald
 
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013Sergey Petrunya
 
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisUniversity of Illinois,Chicago
 
Full Table Scan: friend or foe
Full Table Scan: friend or foeFull Table Scan: friend or foe
Full Table Scan: friend or foeMauro Pagano
 
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisUniversity of Illinois,Chicago
 
Controller Implementation in Verilog
Controller Implementation in VerilogController Implementation in Verilog
Controller Implementation in VerilogAnees Akhtar
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performanceMauro Pagano
 
Adaptive Query Optimization in 12c
Adaptive Query Optimization in 12cAdaptive Query Optimization in 12c
Adaptive Query Optimization in 12cAnju Garg
 
New features-in-mariadb-and-mysql-optimizers
New features-in-mariadb-and-mysql-optimizersNew features-in-mariadb-and-mysql-optimizers
New features-in-mariadb-and-mysql-optimizersSergey Petrunya
 
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...Hsien-Hsin Sean Lee, Ph.D.
 

What's hot (20)

Chasing the optimizer
Chasing the optimizerChasing the optimizer
Chasing the optimizer
 
Instruction set of 8086 Microprocessor
Instruction set of 8086 Microprocessor Instruction set of 8086 Microprocessor
Instruction set of 8086 Microprocessor
 
8086 microprocessor instruction set by Er. Swapnil Kaware
8086 microprocessor instruction set by Er. Swapnil Kaware8086 microprocessor instruction set by Er. Swapnil Kaware
8086 microprocessor instruction set by Er. Swapnil Kaware
 
Les09
Les09Les09
Les09
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c era
 
Microprocessor 8086 instructions
Microprocessor 8086 instructionsMicroprocessor 8086 instructions
Microprocessor 8086 instructions
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
MariaDB 10.0 Query Optimizer
MariaDB 10.0 Query OptimizerMariaDB 10.0 Query Optimizer
MariaDB 10.0 Query Optimizer
 
A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013A few things about the Oracle optimizer - 2013
A few things about the Oracle optimizer - 2013
 
VERILOG CODE
VERILOG CODEVERILOG CODE
VERILOG CODE
 
Oracle 11g caracteristicas poco documentadas 3 en 1
Oracle 11g caracteristicas poco documentadas 3 en 1Oracle 11g caracteristicas poco documentadas 3 en 1
Oracle 11g caracteristicas poco documentadas 3 en 1
 
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
MySQL/MariaDB query optimizer tuning tutorial from Percona Live 2013
 
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency Analysis
 
Full Table Scan: friend or foe
Full Table Scan: friend or foeFull Table Scan: friend or foe
Full Table Scan: friend or foe
 
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency Analysis
 
Controller Implementation in Verilog
Controller Implementation in VerilogController Implementation in Verilog
Controller Implementation in Verilog
 
Same plan different performance
Same plan different performanceSame plan different performance
Same plan different performance
 
Adaptive Query Optimization in 12c
Adaptive Query Optimization in 12cAdaptive Query Optimization in 12c
Adaptive Query Optimization in 12c
 
New features-in-mariadb-and-mysql-optimizers
New features-in-mariadb-and-mysql-optimizersNew features-in-mariadb-and-mysql-optimizers
New features-in-mariadb-and-mysql-optimizers
 
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
 

Viewers also liked

TUGAS AUTOCAD TUTORIAL REGI ANGGARA TKJ X-H
TUGAS AUTOCAD TUTORIAL REGI ANGGARA TKJ X-HTUGAS AUTOCAD TUTORIAL REGI ANGGARA TKJ X-H
TUGAS AUTOCAD TUTORIAL REGI ANGGARA TKJ X-HRegi Anggara
 
Pikkulapsiperhepalvelut esittelyssä
Pikkulapsiperhepalvelut esittelyssäPikkulapsiperhepalvelut esittelyssä
Pikkulapsiperhepalvelut esittelyssäKai Kortelainen
 
Unohtuuko kunnallinen sosiaalipolitiikka kuntatalouden kurimuksessa? fysioter...
Unohtuuko kunnallinen sosiaalipolitiikka kuntatalouden kurimuksessa? fysioter...Unohtuuko kunnallinen sosiaalipolitiikka kuntatalouden kurimuksessa? fysioter...
Unohtuuko kunnallinen sosiaalipolitiikka kuntatalouden kurimuksessa? fysioter...Kai Kortelainen
 
Lapsen tieto ja osallisuus varhaiskasvatuksessa, Koulutussuunnittelija Tiina ...
Lapsen tieto ja osallisuus varhaiskasvatuksessa, Koulutussuunnittelija Tiina ...Lapsen tieto ja osallisuus varhaiskasvatuksessa, Koulutussuunnittelija Tiina ...
Lapsen tieto ja osallisuus varhaiskasvatuksessa, Koulutussuunnittelija Tiina ...Kai Kortelainen
 
Lasten hyvinvoinnin rakentuminen maahanmuuttajaperheissä, Yliopettaja Minna V...
Lasten hyvinvoinnin rakentuminen maahanmuuttajaperheissä, Yliopettaja Minna V...Lasten hyvinvoinnin rakentuminen maahanmuuttajaperheissä, Yliopettaja Minna V...
Lasten hyvinvoinnin rakentuminen maahanmuuttajaperheissä, Yliopettaja Minna V...Kai Kortelainen
 
Asiakaslähtöinen varhaiskasvatus uusissa rakenteissa, Kouvolan kaupungin varh...
Asiakaslähtöinen varhaiskasvatus uusissa rakenteissa, Kouvolan kaupungin	varh...Asiakaslähtöinen varhaiskasvatus uusissa rakenteissa, Kouvolan kaupungin	varh...
Asiakaslähtöinen varhaiskasvatus uusissa rakenteissa, Kouvolan kaupungin varh...Kai Kortelainen
 
Kunnallisen sosiaalipolitiikan sisältö ja arvo, ylisosiaalineuvos Aulikki Kan...
Kunnallisen sosiaalipolitiikan sisältö ja arvo, ylisosiaalineuvos Aulikki Kan...Kunnallisen sosiaalipolitiikan sisältö ja arvo, ylisosiaalineuvos Aulikki Kan...
Kunnallisen sosiaalipolitiikan sisältö ja arvo, ylisosiaalineuvos Aulikki Kan...Kai Kortelainen
 
Päivähoito ja neuvola lapsen hyvinvointia tukemassa ja hyvinvointitietoa tuot...
Päivähoito	ja neuvola	lapsen hyvinvointia tukemassa ja hyvinvointitietoa tuot...Päivähoito	ja neuvola	lapsen hyvinvointia tukemassa ja hyvinvointitietoa tuot...
Päivähoito ja neuvola lapsen hyvinvointia tukemassa ja hyvinvointitietoa tuot...Kai Kortelainen
 
surface computing
surface computingsurface computing
surface computingSunil Sahu
 
Toteutaaako Hyvinvointipalvelujen toimiala kunnallista sosiaalipolitiikkaa, h...
Toteutaaako Hyvinvointipalvelujen toimiala kunnallista sosiaalipolitiikkaa, h...Toteutaaako Hyvinvointipalvelujen toimiala kunnallista sosiaalipolitiikkaa, h...
Toteutaaako Hyvinvointipalvelujen toimiala kunnallista sosiaalipolitiikkaa, h...Kai Kortelainen
 

Viewers also liked (19)

Ok 10 org cpu
Ok 10 org cpuOk 10 org cpu
Ok 10 org cpu
 
TUGAS AUTOCAD TUTORIAL REGI ANGGARA TKJ X-H
TUGAS AUTOCAD TUTORIAL REGI ANGGARA TKJ X-HTUGAS AUTOCAD TUTORIAL REGI ANGGARA TKJ X-H
TUGAS AUTOCAD TUTORIAL REGI ANGGARA TKJ X-H
 
Pikkulapsiperhepalvelut esittelyssä
Pikkulapsiperhepalvelut esittelyssäPikkulapsiperhepalvelut esittelyssä
Pikkulapsiperhepalvelut esittelyssä
 
Ok 6 alu
Ok 6 aluOk 6 alu
Ok 6 alu
 
Unohtuuko kunnallinen sosiaalipolitiikka kuntatalouden kurimuksessa? fysioter...
Unohtuuko kunnallinen sosiaalipolitiikka kuntatalouden kurimuksessa? fysioter...Unohtuuko kunnallinen sosiaalipolitiikka kuntatalouden kurimuksessa? fysioter...
Unohtuuko kunnallinen sosiaalipolitiikka kuntatalouden kurimuksessa? fysioter...
 
String
StringString
String
 
Ok 2 rep data1
Ok 2 rep  data1Ok 2 rep  data1
Ok 2 rep data1
 
Lapsen tieto ja osallisuus varhaiskasvatuksessa, Koulutussuunnittelija Tiina ...
Lapsen tieto ja osallisuus varhaiskasvatuksessa, Koulutussuunnittelija Tiina ...Lapsen tieto ja osallisuus varhaiskasvatuksessa, Koulutussuunnittelija Tiina ...
Lapsen tieto ja osallisuus varhaiskasvatuksessa, Koulutussuunnittelija Tiina ...
 
Ok 1 intro
Ok 1 introOk 1 intro
Ok 1 intro
 
Lasten hyvinvoinnin rakentuminen maahanmuuttajaperheissä, Yliopettaja Minna V...
Lasten hyvinvoinnin rakentuminen maahanmuuttajaperheissä, Yliopettaja Minna V...Lasten hyvinvoinnin rakentuminen maahanmuuttajaperheissä, Yliopettaja Minna V...
Lasten hyvinvoinnin rakentuminen maahanmuuttajaperheissä, Yliopettaja Minna V...
 
Asiakaslähtöinen varhaiskasvatus uusissa rakenteissa, Kouvolan kaupungin varh...
Asiakaslähtöinen varhaiskasvatus uusissa rakenteissa, Kouvolan kaupungin	varh...Asiakaslähtöinen varhaiskasvatus uusissa rakenteissa, Kouvolan kaupungin	varh...
Asiakaslähtöinen varhaiskasvatus uusissa rakenteissa, Kouvolan kaupungin varh...
 
Pertemuan IV Teori
Pertemuan IV TeoriPertemuan IV Teori
Pertemuan IV Teori
 
Ok 11 operasi cpu
Ok 11 operasi  cpuOk 11 operasi  cpu
Ok 11 operasi cpu
 
Ok 5 float
Ok 5 floatOk 5 float
Ok 5 float
 
Kunnallisen sosiaalipolitiikan sisältö ja arvo, ylisosiaalineuvos Aulikki Kan...
Kunnallisen sosiaalipolitiikan sisältö ja arvo, ylisosiaalineuvos Aulikki Kan...Kunnallisen sosiaalipolitiikan sisältö ja arvo, ylisosiaalineuvos Aulikki Kan...
Kunnallisen sosiaalipolitiikan sisältö ja arvo, ylisosiaalineuvos Aulikki Kan...
 
Päivähoito ja neuvola lapsen hyvinvointia tukemassa ja hyvinvointitietoa tuot...
Päivähoito	ja neuvola	lapsen hyvinvointia tukemassa ja hyvinvointitietoa tuot...Päivähoito	ja neuvola	lapsen hyvinvointia tukemassa ja hyvinvointitietoa tuot...
Päivähoito ja neuvola lapsen hyvinvointia tukemassa ja hyvinvointitietoa tuot...
 
surface computing
surface computingsurface computing
surface computing
 
Toteutaaako Hyvinvointipalvelujen toimiala kunnallista sosiaalipolitiikkaa, h...
Toteutaaako Hyvinvointipalvelujen toimiala kunnallista sosiaalipolitiikkaa, h...Toteutaaako Hyvinvointipalvelujen toimiala kunnallista sosiaalipolitiikkaa, h...
Toteutaaako Hyvinvointipalvelujen toimiala kunnallista sosiaalipolitiikkaa, h...
 
Teknologi multimedia
Teknologi multimediaTeknologi multimedia
Teknologi multimedia
 

Similar to 5asm the stackandsubroutines

11thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp0111thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp01Karam Abuataya
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsAsuka Nakajima
 
Chapter 2 Part2 C
Chapter 2 Part2 CChapter 2 Part2 C
Chapter 2 Part2 Cececourse
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
Ecet 340 Your world/newtonhelp.com
Ecet 340 Your world/newtonhelp.comEcet 340 Your world/newtonhelp.com
Ecet 340 Your world/newtonhelp.comamaranthbeg100
 
Ecet 340 Motivated Minds/newtonhelp.com
Ecet 340 Motivated Minds/newtonhelp.comEcet 340 Motivated Minds/newtonhelp.com
Ecet 340 Motivated Minds/newtonhelp.comamaranthbeg60
 
Ecet 340 Extraordinary Success/newtonhelp.com
Ecet 340 Extraordinary Success/newtonhelp.comEcet 340 Extraordinary Success/newtonhelp.com
Ecet 340 Extraordinary Success/newtonhelp.comamaranthbeg120
 
Ecet 340 Education is Power/newtonhelp.com
Ecet 340 Education is Power/newtonhelp.comEcet 340 Education is Power/newtonhelp.com
Ecet 340 Education is Power/newtonhelp.comamaranthbeg80
 
Vlsi ii project presentation
Vlsi ii project presentationVlsi ii project presentation
Vlsi ii project presentationRedwan Islam
 
2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docxtarifarmarie
 
02 - Introduction to the cdecl ABI and the x86 stack
02 - Introduction to the cdecl ABI and the x86 stack02 - Introduction to the cdecl ABI and the x86 stack
02 - Introduction to the cdecl ABI and the x86 stackAlexandre Moneger
 
New SQL features in latest MySQL releases
New SQL features in latest MySQL releasesNew SQL features in latest MySQL releases
New SQL features in latest MySQL releasesGeorgi Sotirov
 

Similar to 5asm the stackandsubroutines (20)

ERTS UNIT 3.ppt
ERTS UNIT 3.pptERTS UNIT 3.ppt
ERTS UNIT 3.ppt
 
chapter 4
chapter 4chapter 4
chapter 4
 
MPMC Unit-3 PPT.pdf
MPMC  Unit-3 PPT.pdfMPMC  Unit-3 PPT.pdf
MPMC Unit-3 PPT.pdf
 
11thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp0111thingsabout11g 12659705398222 Phpapp01
11thingsabout11g 12659705398222 Phpapp01
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
 
COA_mod2.ppt
COA_mod2.pptCOA_mod2.ppt
COA_mod2.ppt
 
Real to protected_mode
Real to protected_modeReal to protected_mode
Real to protected_mode
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Chapter8.ppt
Chapter8.pptChapter8.ppt
Chapter8.ppt
 
Chapter 2 Part2 C
Chapter 2 Part2 CChapter 2 Part2 C
Chapter 2 Part2 C
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Ecet 340 Your world/newtonhelp.com
Ecet 340 Your world/newtonhelp.comEcet 340 Your world/newtonhelp.com
Ecet 340 Your world/newtonhelp.com
 
Ecet 340 Motivated Minds/newtonhelp.com
Ecet 340 Motivated Minds/newtonhelp.comEcet 340 Motivated Minds/newtonhelp.com
Ecet 340 Motivated Minds/newtonhelp.com
 
Ecet 340 Extraordinary Success/newtonhelp.com
Ecet 340 Extraordinary Success/newtonhelp.comEcet 340 Extraordinary Success/newtonhelp.com
Ecet 340 Extraordinary Success/newtonhelp.com
 
Ecet 340 Education is Power/newtonhelp.com
Ecet 340 Education is Power/newtonhelp.comEcet 340 Education is Power/newtonhelp.com
Ecet 340 Education is Power/newtonhelp.com
 
Vlsi ii project presentation
Vlsi ii project presentationVlsi ii project presentation
Vlsi ii project presentation
 
2.1 ### uVision Project, (C) Keil Software .docx
2.1   ### uVision Project, (C) Keil Software    .docx2.1   ### uVision Project, (C) Keil Software    .docx
2.1 ### uVision Project, (C) Keil Software .docx
 
System Software
System SoftwareSystem Software
System Software
 
02 - Introduction to the cdecl ABI and the x86 stack
02 - Introduction to the cdecl ABI and the x86 stack02 - Introduction to the cdecl ABI and the x86 stack
02 - Introduction to the cdecl ABI and the x86 stack
 
New SQL features in latest MySQL releases
New SQL features in latest MySQL releasesNew SQL features in latest MySQL releases
New SQL features in latest MySQL releases
 

5asm the stackandsubroutines

  • 1. CEG 320/520: Computer Organization and The Stack and The Stack and Subroutines
  • 2. CEG 320/520: Computer Organization and The Stack and The Stack: Introduction • A7 is a special address register, called the stack pointer. • When programming assembly, we can use SP as an alias for A7. MOVEA.L #$3000,SP • In the simulator, it is also called US (user stack pointer) • There is also a supervisor stack pointer, but we won’t worry about it yet. • Since our program usually starts at a low memory address and grows upward, we start the stack at a high memory address and work downward.
  • 3. CEG 320/520: Computer Organization and The Stack and The Stack: Introduction • We push values onto the stack using predecrement mode – MOVE.W D2,-(SP) – MOVE.W D3,-(SP) • We pop values from the stack using postincrement mode – MOVE.W (SP)+, D3 – MOVE.W (SP)+, D2 • Some instructions affect the stack directly
  • 4. CEG 320/520: Computer Organization and The Stack and The Stack: Operations (push/pop) $7000 $6FFE $6FFC main MOVE.W #12,-(A7) MOVE.W #20,-(A7) MOVE.W #30,-(A7) … MOVE.W (A7)+,D0 MOVE.W (A7)+,D1 MOVE.W (A7)+,D2 PUSH(12) Last-in, first-out PUSH(20) (LIFO) PUSH(30) POP = 30 POP = 20 POP = 12
  • 5. CEG 320/520: Computer Organization and The Stack and The Stack: Purposes • Temporary storage of variables • Temporary storage of program addresses • Communication with subroutines – Push variables on stack – Jump to subroutine – Clean stack – Return
  • 6. CEG 320/520: Computer Organization and The Stack and Programming Subroutines • Why use subroutines? – Code re-use – Easier to understand code (readability) – Divide and conquer • Complex tasks are easier when broken down into smaller tasks • How do we call a subroutine in assembly? – Place the parameters somewhere known – JSR or BSR to jump to the subroutine – RTS to return
  • 7. CEG 320/520: Computer Organization and The Stack and C Assembly main MOVE.W A,D1 JSR sqr MOVE.W D0,B move.w #228,D7 TRAP #14 ;*** subroutine sqr *** sqr MUL.W D1,D1 MOVE.W D1,D0 RTS ORIGIN $2000 A DC.W 5 B DS.W 1 end main() { int a, b; a = 5; b = sqr(a); printf(“%dn” b); } /* subrtn sqr */ int sqr(int val) { int sqval; sqval = val * val; return sqval; }
  • 8. CEG 320/520: Computer Organization and The Stack and JSR and BSR • JSR label – MOVE.L PC, –(SP) – LEA address-of-label, PC In other words: – SP ← [SP] – 4 – [SP] ← [PC] – PC ← address-of-label • BSR label – Same, but offset from the current PC is stored instead of the absolute address
  • 9. CEG 320/520: Computer Organization and The Stack and JSR example 1000 MOVE.L #5,D1 1006 LEA ARRAY,A0 100C JSR SUMARR 1012 MOVE.L D0,SUM 1018 MOVE.L #228, D7 101E TRAP #14 1020 SUMARR CLR.L D0 … RTS ARRAY DC.L 12,15,31 SUM DS.L 1 END $7000 00001012PC ? 1012 0000 $6FFE $6FFC 00007000A7
  • 10. CEG 320/520: Computer Organization and The Stack and RTS • RTS • Pop the address from the stack back into the PC – MOVE.L (SP)+, PC In other words: – PC ← [SP] – [SP] ← [SP] + 4
  • 11. CEG 320/520: Computer Organization and The Stack and RTS example 1000 MOVE.L #5,D1 1006 LEA ARRAY,A0 100C JSR SUMARR 1012 MOVE.L D0,SUM 1018 MOVE.L #228, D7 101E TRAP #14 1020 SUMARR CLR.L D0 … 1032 RTS ARRAY DC.L 12,15,31 SUM DS.L 1 END $7000 ? 1012 0000 $6FFE $6FFC 00001034PC 00006FFCA7
  • 12. CEG 320/520: Computer Organization and The Stack and Passing parameters in registers main MOVE.W A,D1 JSR sqr MOVE.W D0,B move.w #228,D7 TRAP #14 ; sqr MOVE.W D1,D0 MULS.W D0,D0 RTS ORIGIN $2000 A DC.W 5 B DS.W 1 end • The number to be squared is in D1. • The result is returned in D0, D1 is unchanged.
  • 13. CEG 320/520: Computer Organization and The Stack and Parameter passing on the stack • If we use registers to pass our parameters: – Limit of 7 parameters to/from any subroutine. – We use up registers so they are not available to our program. • So, instead we push the parameters onto the stack. • Our conventions: – Parameters are passed on the stack – One return value can be provided in D0. – D0, D1, A0, A1 can be used by a subroutine. Other registers must first be saved.
  • 14. CEG 320/520: Computer Organization and The Stack and First things first… • Both the subroutine and the main program must know how many parameters are being passed! – In C we would use a prototype: int power (int number, int exponent); • In assembly, you must take care of this yourself. • After you return from a subroutine, you must also clear the stack. You have to clean up your mess.
  • 15. CEG 320/520: Computer Organization and The Stack and Passing parameters on the stack Mul3 – multiply three numbers and place the result in D0.Mul3 – multiply three numbers and place the result in D0. ; ******** Main Program ******************************** 1000 START MOVE.W NUM1,-(SP) ;Push first param 1006 MOVE.W NUM2,-(SP) ;Push 2nd param 100C MOVE.W NUM3,-(SP) ;Push 3rd param 1012 JSR MUL3 1018 ADDA.L #6,SP ;Clean the stack! 101E MOVE.W #228,D7 1020 TRAP #14 ; ******* Subroutine Mul3 ****************************** 1022 MUL3 MOVE.W 4(SP),D0 ;D0 = NUM3 1026 MULS.W 6(SP),D0 ;D0 *= NUM2 102A MULS.W 8(SP),D0 ;D0 *= NUM1 102E RTS ;SP --> rtrn addr! ORG $2000 2000 NUM1 DC.W 5 2002 NUM2 DC.W 8 2004 NUM3 DC.W 2 END
  • 16. CEG 320/520: Computer Organization and The Stack and Know how to… • Push parameters onto the stack • Access parameters on the stack using indexed addressing mode • Draw the stack to keep track of subroutine execution – Parameters – Return address • Clean the stack after a subroutine call
  • 17. CEG 320/520: Computer Organization and The Stack and Review problem ; ******** Main Program ********* 1000 START MOVE.L NUM1,-(SP) 1006 MOVE.L NUM2,-(SP) 100C MOVE.L NUM3,-(SP) 1012 JSR SUB1 1018 Next instr… … … ; ******* Subroutine SUB1 ******* 1022 SUB1 … 1026 … 102E RTS ORG $2000 2000 NUM1 DC.L $150 2004 NUM2 DC.L $180 2008 NUM3 DC.L $12 END
  • 18. CEG 320/520: Computer Organization and The Stack and Writing transparent subroutines • A transparent subroutine doesn’t change any registers except D0, D1, A0 and A1. • If we need more registers than this, we must save the register values when we enter the subroutine and restore them later. • Where do we store them? You guessed it: the stack. • The 68000 provides a convenient instruction, MOVEM, to push the contents of several registers to the stack at one time.
  • 19. CEG 320/520: Computer Organization and The Stack and A transparent subroutine subr1 MOVEM.L D2-D3/A2-A3,-(SP) MOVE.L #32,D2 MOVE.L 20(SP),D3 … MOVEM.L (SP)+,D2-D3/A2-A3 RTS $7000 ? rtrn rtrn $6FFE $6FFC D2 D2 D3 D3 A2 A2 A3 A3 P1 P2 We can now safely modify D2, D3,We can now safely modify D2, D3, A2 and A3, knowing that we willA2 and A3, knowing that we will restore their original contents later.restore their original contents later. We saved 4 registers, so the lastWe saved 4 registers, so the last parameter lives at SP + (4parameter lives at SP + (4×4) + 4.×4) + 4. (4 bytes/reg + 4 for the return addr.)(4 bytes/reg + 4 for the return addr.)
  • 20. CEG 320/520: Computer Organization and The Stack and Review problem #2 ; ******** Main Program ********* 1000 START MOVE.L NUM1,-(SP) 1006 MOVE.L NUM2,-(SP) 100C MOVE.L NUM3,-(SP) 1012 JSR SUB1 1018 Next instr… … … ; ******* Subroutine SUB1 ******* 1022 SUB1 MOVEM.L D2-D3/A4,-(SP) 1026 … ; show stack 102E MOVEM.L (SP)+,D2-D3/A4 1032 RTS ORG $2000 2000 NUM1 DC.L $150 2004 NUM2 DC.L $180 2008 NUM3 DC.L $12 END
  • 21. CEG 320/520: Computer Organization and The Stack and Know how to… • Write transparent subroutines • Draw the stack and access parameters on the stack, taking into account the saved register values and the subroutine return address.
  • 22. CEG 320/520: Computer Organization and The Stack and Passing by value & reference • We pushed the value of NUM1, NUM2, and NUM3 on the stack. • What if we want to change the input parameter values? • For example, what if we want to write a subroutine that will multiply all of its arguments’ values by 2, actually changing the values in memory? • We must pass the parameters by reference…
  • 23. CEG 320/520: Computer Organization and The Stack and The PEA instruction • PEA – Push effective address PEA label is the same as… LEA label,A0 MOVEA.L A0,-(SP) but without using A0.
  • 24. CEG 320/520: Computer Organization and The Stack and Passing parameters by reference dbl3 – double the values of three parametersdbl3 – double the values of three parameters ; ******** Main Program ********* 1000 START PEA NUM1 1006 PEA NUM2 100C PEA NUM3 1012 JSR dbl3 1018 ADDA.L #12,SP 101E MOVE.W #228,D7 1020 TRAP #14 ORG $2000 2000 NUM1 DC.W 5 2002 NUM2 DC.W 8 2004 NUM3 DC.W 2 END $7000 ? 2002 0000 $6FFE $6FFC 2004 0000 1018 0000 2000 0000
  • 25. CEG 320/520: Computer Organization and The Stack and Using parameters passed by reference dbl3 – double the values of three parametersdbl3 – double the values of three parameters ; ******* Subroutine dbl3 ********* DBL3 MOVEA.L 4(SP),A0 MOVE.W (A0),D0 MULS.W #2,D0 MOVE.W D0,(A0) MOVEA.L 8(SP),A0 … ; repeat for each RTS ORG $2000 2000 NUM1 DC.W 5 2002 NUM2 DC.W 8 2004 NUM3 DC.W 2 END ? 2002 0000 2004 0000 1018 0000 2000 0000 $7000 $6FFE $6FFC
  • 26. CEG 320/520: Computer Organization and The Stack and Putting it all together MAIN MOVE.W COUNT,-(SP) PEA ARRAY JSR SUB2 … SUB2 MOVEM.L D2-D3,-(SP) ;HERE ORG $2000 COUNT DC.W 105 ARRAY DC.W 10,12,3,7,9,18 • What does the stack look like at the line ;HERE ?
  • 27. CEG 320/520: Computer Organization and The Stack and Characteristics of good subroutines • Generality – can be called with any arguments – Passing arguments on the stack does this. • Transparency – you have to leave the registers like you found them, except for D0, D1, A0, and A1. – We use the MOVEM instruction for this purpose. • Readability – well documented. – See the example handout. • Re-entrant – subroutine can call itself if necessary – This is done using stack frames, something we will look at in a few days…
  • 28. CEG 320/520: Computer Organization and The Stack and Know how to… • Pass parameters by value and by reference • Use address registers to access parameters passed by reference • Write general, transparent, readable subroutines