assembly language - kirkwood...assembly language part 2 stack-relative addressing • with...

42
Assembly Language Part 2

Upload: others

Post on 28-Jun-2020

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Assembly Language

Part 2

Page 2: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Stack-relative addressing

• With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array, with the instruction’s operand specifier as the index

• In other words, the operand value is found at the address, which is the sum of the stack pointer’s value and the value of the operand specifier, as shown below: operand value = value at SP[op specifier]

Page 3: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Stack-relative addressing • A stack is not quite the same thing as an array

– If you were putting several items in an array, you would probably put the first item at index 0, the second at 1, etc., until the nth item is placed at address n-1

– Think of a stack as a backwards array –the nth item is placed at 0, and the first item at n-1

• Another (and perhaps more correct) way of thinking of the Pep/8 runtime stack, is that it grows upward in memory – that is, if two 2-byte values are placed on the stack beginning at address 0008, the second value would be at 0006 (instead of 000A)

Page 4: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Stack-oriented instructions

• The ADDSP and SUBSP instructions are used to add and subtract values to (or from) the stack pointer

• Remember, the stack grows upward – so SUBSP allocates stack storage and ADDSP deallocates it

• The example on the next slide illustrates (not especially efficient) use of the runtime stack to store data

Page 5: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Example lda 'h', i ; push 'h' on stack stbytea -1, s lda 'e', i ; push 'e' stbytea -2, s lda 'y', i ; push 'y' stbytea -3, s lda '!', i ; push '!' stbytea -4, s subsp 4, i ; announce 4 bytes are taken charo 3, s ; output 'h' charo 2, s ; output 'e' charo 1, s ; output 'y' charo 0, s ; output '!' addsp 4, i ; let go of stack storage stop .end

Page 6: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

All that for …?

• The previous example was just an illustration; the runtime stack is more commonly used for allocation of temporary memory during the run of a program, specifically for: – parameter & local variable allocation – procedure calls

Page 7: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Procedure (method) calls Consider the following Java program: public class Message { public static void printMsg() { System.out.print(“Hello”); System.out.println(); } public static void main (String [] args) { printMsg(); printMsg(); printMsg(); } }

Simplest version of method calling in Java: - printMsg is void & takes no arguments - printMsg & main are static, so we don’t need to create an object

Page 8: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Equivalent Pep/8 program ;File: voidNoParams BR main ; ;******* void printMsg () printMsg: STRO msg,d CHARO '\n',i RET0 msg: .ASCII "Hello\x00" ; ;******* int main () main: CALL printMsg ; CALL printMsg ; CALL printMsg ; STOP .END

The main method uses the CALL command to request execution of printMsg CALL is something like an unconditonal branch in this instance, since there isn’t any memory allocated for parameters The printMsg method contains a RET instruction – in this case, RET0 • the 0 refers to the # of local variables • RET comes in versions RET0 … RET7 • RETn in general: n is # of bytes occupied by local variables

Page 9: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 and procedure calls

• Although not immediately obvious from the previous example, both the CALL and RETn instructions use the system stack

• For this simple example, the stack is used to store the return address; when local variables are involved, the stack also stores these

• The next two slides illustrate how CALL and RETn work with respect to the stack

Page 10: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 and procedure calls

• CALL instruction: SP = SP – 2 ; decrement stack pointer – the ; stack grows up from address ; FBCF, so we’re moving 2 bytes ; up (size of an address) to FBCD Mem[SP] = PC; store current value of program ; counter, get ready for branch PC = Operand; branch to location specified ; by operand

Page 11: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 and procedure calls

• RETn instruction: SP = SP + n ; At this point, we’re returning ; from a method – incrementing ; stack pointer by n bytes ; effectively deallocates memory ; for n local variables PC = Mem[SP] ; recall stored return address SP = SP + 2 ; complete pop from stack (give ; back memory address occupied)

Page 12: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Parameter passing

• In general, high level languages allow two types of parameter passing: – pass by value: a copy of the argument’s value is

assigned to the corresponding parameter – pass by reference: the parameter is a pointer to the

address of the original argument’s value • Java uses both, but with less programmer

control than is available in C++: – primitives are always passed by value – objects are always passed by reference

Page 13: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pass-by-value example public void Printer { public static void line (int n) { int c; for(c=0; c<n; c++) Sop(“*”); Sopln(); } public static void main (String [] args) { line(7); } }

Page 14: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 equivalent ;File: voidWithArg BR main ; ;******* void line (int n) n: .EQUATE 4 ;formal parameter c: .EQUATE 0 ;local variable line: SUBSP 2,i ;allocate local LDA 0,i ;for (c = 0 STA c,s for: CPA n,s ;c < n BRGE endFor CHARO '*',i ; LDA c,s ;c++ ADDA 1,i STA c,s BR for endFor: CHARO ‘\n',i ; RET2 ; deallocate local, ; pop retAddr

;******* main () main: LDA 7, i ; begin procedure call STA -2, s ; get ready to push arg SUBSP 2, i ; push arg CALL line ; push return address ADDSP 2, i ; pop arg STOP .END

Page 15: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

What’s going on here

• Calling procedure (main) pushes arg(s) on stack with SUBSP, then pushes return address on stack with CALL

• Called procedure (line) allocates space for local variables with SUBSP, executes its body, deallocates local variables and pops return address with RETn

• Calling procedure pops arg(s) with ADDSP

Page 16: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Reference parameter/global variable model

• C++ reference parameters are references to the actual arguments (as opposed to copies of the values)

• At assembly language level, the address of the actual argument is pushed on the stack – in the previous example, the actual arguments

were global variables; therefore we could use the name of the corresponding symbol to refer to the address

– Code used to push the address of global variable is a load instruction with immediate addressing

Page 17: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

A program with reference parameters

#include <iostream.h> int a, b; // global variables void swap (int& r, int& s) { int temp; temp = r; r = s; s = temp; } void order (int& x, int& y) { if (x > y) { swap (x, y); } // ra2 }

int main () { cout << "Enter an integer: "; cin >> a; cout << "Enter an integer: "; cin >> b; order (a, b); cout << "Ordered they are: " << a << ", " << b << endl; // ra1 return 0; }

Page 18: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 equivalent ;******* main () main: STRO msg1,d DECI a,d ;cin >> a STRO msg1,d DECI b,d ;cin >> b LDA a,i ;push address of a STA -2,s LDA b,i ;push address of b STA -4,s SUBSP 4,i ;push params CALL order ;order (a, b) ra1: ADDSP 4,i ;pop params STRO msg2,d DECO a,d STRO msg3,d DECO b,d CHARO '\n',i STOP

msg1: .ASCII "Enter an integer: \x00" msg2: .ASCII "Ordered they are: \x00" msg3: .ASCII ", \x00" .END

int main () { cout << "Enter an integer: "; cin >> a; cout << "Enter an integer: "; cin >> b; order (a, b); cout << "Ordered they are: " << a << ", " << b << endl; // ra1 return 0; }

Page 19: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 equivalent x: .EQUATE 4 ;formal parameter y: .EQUATE 2 ;formal parameter order: LDA x,sf ;if (x > y) CPA y,sf BRLE endIf LDA x,s ; push x STA -2,s LDA y,s ; push y STA -4,s SUBSP 4,i ; push params CALL swap ; swap (x, y) ADDSP 4,i ; pop params endIf: RET0 ;pop retAddr

void order (int& x, int& y) { if (x > y) { swap (x, y); } // ra2 }

• This procedure (order) calls a second procedure, swap • The order procedure received two reference parameters, x and y, whose addresses are already stored on the runtime stack • Since the swap procedure is also expecting reference parameters, the order procedure simply passes on the address of x to swap

Page 20: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 equivalent BR main a: .BLOCK 2 ;global variable b: .BLOCK 2 ;global variable ; ;******* void swap (int& r, int& s) r: .EQUATE 6 ;formal parameter s: .EQUATE 4 ;formal parameter temp: .EQUATE 0 ;local variable swap: SUBSP 2,i ;allocate local LDA r,sf ;temp = r STA temp,s LDA s,sf ;r = s STA r,sf LDA temp,s ;s = temp STA s,sf RET2 ;deallocate local, ;pop retAddr

int a, b; // global variables void swap (int& r, int& s) { int temp; temp = r; r = s; s = temp; }

• Procedure swap needs to translate the line temp = r; into assembly language – meaning, it must access the value of a parameter whose address is on the runtime stack • The mechanism for this access is stack-relative deferred addressing

Page 21: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Yet another addressing mode • Recall that the relation between the operand and

its specifier in stack-relative addressing is: operand = Memory[SP + operand specifier] – the key point is, the operand itself is stored on the

runtime stack – with reference parameters, the operand’s address is

on the stack • The addressing mode in use is stack-relative

deferred addressing, in which the operand/specifier relation is: operand = Memory[Memory[SP + operand specifier]]

Page 22: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

• Argument is pushed on the stack via a load instruction with immediate mode addressing: LDA a, i ; copy address of variable a into accumulator STA -2, s ; store address on stack 2 bytes up from SP

• Formal parameter is accessed using stack-relative deferred addressing: LDA r, sf ; temp = r; (r points to x which points to a) STA temp, s ; reference parameter on right side of = ; with local variable on left side LDA s, sf ; r = s; (s points to y which points to b) STA r, sf ; reference parameters on both sides of =

Translating pass-by-reference calls involving global variables: summary

Page 23: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

More common situation: reference parameter with local variables

// C++ program with method for finding perimeter of rectangle #include <iostream.h> void rect (int& p, int w, int h) { p = (w + h) * 2; } int main () { int perim, width, height; cout << "Enter width: "; cin >> width; cout << "Enter height: "; cin >> height; rect (perim, width, height); // ra1 cout << "perim = " << perim << endl; return 0; }

Page 24: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 equivalent • Procedure rect uses its

reference parameter, p, as we saw with the previous example: STA p, sf stores the value in A using stack-relative deferred addressing

• Note the line before that: what does ASLA do?

;File: fig0629.pep ;Computer Systems ;Figure 6.29 ; BR main ; ;******* void rect (int& p, int w, int h) p: .EQUATE 6 ;formal parameter w: .EQUATE 4 ;formal parameter h: .EQUATE 2 ;formal parameter rect: LDA w,s ;p = (w + h) * 2 ADDA h,s ASLA STA p,sf endIf: RET0 ;pop retAddr ;

Page 25: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 equivalent: first part of main

• Local variables perim, width and height are pushed on runtime stack

• The symbol perim is not the absolute address of its value, as it was in the global case; its value (4) is only the address relative to the stack top – so can’t use:

LDA perim, i STA -2, s

• The instructions: MOVSPA ADDA perim, i STA -2, S are used to : – copy SP value to A – Add value of perim (4) to A – Put address of perim in stack cell that will

be the reference parameter, p

;******* main () perim: .EQUATE 4 ;local variable width: .EQUATE 2 ;local variable height: .EQUATE 0 ;local variable main: SUBSP 6,i ;allocate locals STRO msg1,d DECI width,s STRO msg2,d DECI height,s MOVSPA ;push address of perim ADDA perim,i STA -2,s LDA width,s ;push value of width STA -4,s LDA height,s ;push value of height STA -6,s SUBSP 6,i ;push params

Page 26: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Pep/8 equivalent: rest of main CALL rect ;rect (perim, width, height) ra1: ADDSP 6,i ;pop params STRO msg3,d DECO perim,s CHARO '\n',i ADDSP 6,i ;deallocate locals STOP msg1: .ASCII "Enter width: \x00" msg2: .ASCII "Enter height: \x00" msg3: .ASCII "perim = \x00" .END

Page 27: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Value-returning method • The previous example was typical in the

sense that most programs use local variables rather than global, but atypical in its use of a reference parameter in a void method to change one value

• It is usual in this sort of situation (one value to be changed) to write a function – that is, a value-returning method or procedure

• Your text illustrates a value-returning method with an added twist: recursion

Page 28: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Binomial coefficient program: C++ #include <iostream> using namespace std; int binCoeff (int n, int k) { int y1, y2; if ((k == 0) || (n == k)) { return 1; } else { y1 = binCoeff (n - 1, k); // ra2 y2 = binCoeff (n - 1, k - 1); // ra3 return y1 + y2; } }

int main () { cout << "binCoeff (3, 1) = " << binCoeff (3, 1); // ra1 cout << endl; return 0; } /*********************************** Computes binomial coefficients using Pascal’s triangle; in this example, the main method tests the (recursive) binCoeff method using a 3rd-degree polynomial: result is the coefficient for the 2nd term (1) of the expansion of (x+y)3

***********************************/

Page 29: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Binomial Coefficient: Pep8 BR main ;1st instruction takes us past setup code below ; ; initial setup: .EQUATE pseudo-ops used to ; represent offset from stack top for stack-relative ; addressing ; ;******* int binomCoeff (int n, int k) retVal: .EQUATE 10 ;returned value #2d n: .EQUATE 8 ;formal parameter #2d k: .EQUATE 6 ;formal parameter #2d y1: .EQUATE 2 ;local variable #2d y2: .EQUATE 0 ;local variable #2d

Page 30: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Binomial Coefficient: Pep8 ;******* main () main: STRO msg,d ;cout << "binCoeff (3, 1) = " LDA 3,i ;push 3 STA -4,s LDA 1,i ;push 1 STA -6,s SUBSP 6,i ;push #retVal #n #k CALL binCoeff ;binomCoeff (3, 1) ra1: ADDSP 6,i ;pop #k #n #retVal DECO -2,s ; << binCoeff (3, 1) CHARO '\n',i ;cout << endl STOP msg: .ASCII "binCoeff (3, 1) = \x00" .END

Page 31: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Binomial Coefficient: Pep8 ; first part of binCoeff, rearranged to (try to) illuminate the logic binCoeff:SUBSP 4,i ;allocate #y1 #y2 if: LDA k,s ;if ((k == 0) BREQ then ; …(some code omitted here) ; option 1: stop condition reached because k==0 ; so return 1, store return value on stack & ; deallocate some stack memory then: LDA 1,i ;return 1 STA retVal,s RET4 ;deallocate #y2 #y1, pop retAddr

Page 32: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Binomial Coefficient: Pep8 ; omitted lines from previous slide; execute this code if k != 0 ; check here for second option, if n==k LDA n,s ;|| (n == k)) CPA k,s BRNE else ; if branch does not occur, fall through to same code as previous slide: then: LDA 1,i ;return 1 STA retVal,s RET4 ;deallocate #y2 #y1, pop retAddr

Page 33: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Binomial Coefficient: Pep8 ; else option: if we haven’t hit either base case (k==1 or n==k) ; go to recursive case: decrement n, then get stack set up for ; recursive call: else: LDA n,s ;push n - 1 SUBA 1,i STA -4,s LDA k,s ;push k STA -6,s SUBSP 6,i ;push #retVal #n #k CALL binCoeff ;binomCoeff (n - 1, k) ; important note: instruction after this call is label ra2 – this is ; where program control will return when we finish all the ; business triggered by the recursive call we just made

Page 34: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Binary Coefficient: Pep8 ; ra2 represents the return address to go back to once the ; initial recursive call (binomCoeff (n - 1, k) in C++) has ; finished – RET4 from either the “then” or the “else” option ; will pop ra2 as return address from the stack ra2: ADDSP 6,i ;pop #k #n #retVal LDA -2,s ;y1 = binomCoeff (n - 1, k) STA y1,s LDA n,s ;push n - 1 SUBA 1,i STA -4,s LDA k,s ;push k - 1 SUBA 1,i STA -6,s SUBSP 6,i ;push #retVal #n #k CALL binCoeff ;binomCoeff (n - 1, k - 1) ; note we have a second recursive call here – when we return from it, ; we’ll go to the next line (labeled ra3)

Page 35: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Binary Coefficient: Pep8 ; This code gives us the cleanup after the last recursive call (some version of ; y1 = binomCoeff (n - 1, k) in C++). Here we make sure the last result is ; placed in retVal, then we deallocate the local variables one last time and ; pop the return address (which will take us back to main (ra1) this time) ra3: ADDSP 6,i ;pop #k #n #retVal LDA -2,s ;y2 = binomCoeff (n - 1, k - 1) STA y2,s LDA y1,s ;return y1 + y2 ADDA y2,s STA retVal,s endIf: RET4 ;deallocate #y2 #y1, pop retAddr

Page 36: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Another example: multiplication

• Pep8 offers add and subtract instructions, but it doesn’t have multiplication or division (standard or modulus)

• Recall that we can multiply or divide by 2 using arithmetic shift instructions

• The next example illustrates a multiplication algorithm that utilizes multiplication and division (both kinds) by 2

Page 37: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Product function: C++ int product (int n1, int n2) { int result = 0; while (n1 > 0) { if (n1 % 2 == 1) // AND with x0001 - check Z flag result += n2; n1 /= 2; // ASR n2 *= 2; // ASL } return result; }

Page 38: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Product program: Pep8 ; As with the previous example, a little setup happens before branch to main ; In this case, although the stack is used, parameter passing is not ; standard, and stack offsets are not pre-calculated ; br main prompt: .ascii "Enter value for operand: \x00" equals: .ascii " = \x00" times: .ascii " x \x00"

Page 39: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Product program: Pep8 ; ***************** main function a: .block 2 ;local variables b: .block 2 main: stro prompt, d deci a, d ;cin >> a; stro prompt, d deci b, d ;cin >> b; lda a, d sta -4, s ;push a on stack lda b, d sta -6, s ;push b on stack

subsp 6 , I ;move sp past call product ;data & call fn addsp 6, i deco a, d cout << a stro times, d deco b, d cout << b stro equals, d deco -2, s cout << fn result charo '\n', i stop .end

Page 40: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Product program: Pep8 ; ************************* product function result: .equate 6 n2: .equate 4 n1: .equate 2 product: lda 0, i ; get ready to initialize result sta result, s ; result = 0 while: lda n1, s ; set up while loop condition cpa 0, i ; while (n1 != 0) breq endloop lda n1, s anda 0x0001, i ; if (n1 % 2 == 1) breq shift lda result, s adda n2, s sta result, s

shift: lda n1, s ; get ready to divide by 2 asra ; n1 / 2 sta n1, s ; n1 = lda n2, s ; get ready to multiply by 2 asla ; n2 * 2 sta n2, s ; n2 = br while endloop: ret0

Page 41: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

Translating boolean values

• C++ treats boolean values as 0 (meaning false) and anything else (meaning true)

• Java treats these values similarly, if not transparently

• At the bit level, it would seem obvious to extend this practice: – 0000 means false – 0001 means true

Page 42: Assembly Language - Kirkwood...Assembly Language Part 2 Stack-relative addressing • With stack-relative addressing, the stack pointer acts sort of like the base pointer of an array,

The complications of convenience

• Consider the basic logical operations AND, OR and NOT – 0000 AND 0001 is 0000 (false, as it should be) – 0000 OR 0001 is 0001 (true, also as expected) – NOT 0000 is 1111 – oops…

• Many architectures resolve this by using XOR instead of NOT, since – p 0 = p and – p 1 = !p

– Interestingly, Pep8 doesn’t have XOR