Course Hero Logo

Handout5 - COMP 160 COMPUTER ORGANIZATION Week 7~8 (Ch 5)...

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 17 out of 82 pages.

COMP 160COMPUTERORGANIZATIONWeek 7~8 (Ch 5)ProceduresInstructor: Ms. Laura Lo
CHAPTEROVERVIEW¢Stack Operations¢Defining and Using Procedures¢Linking to an External Library¢The Irvine32 Library¢64-Bit Assembly Programming
STACKOPERATIONS¢Runtime Stack¢PUSH Operation¢POP Operation¢PUSH and POP Instructions¢Using PUSH and POP¢Example: Reversing a String¢Related Instructions
RUNTIMESTACK¢Imagine a stack of plates . . .plates are only added to the topplates are only removed from the topLIFO structure12345678910topbottom
RUNTIMESTACK(32-BITMODE)§The runtime stack is a memory array manageddirectly by the CPU.§Use the ESP (extended stack pointer) register. ESPalways points to the top of stack (FILO)§The stack grows down from high memory to lowmemory.* SP in Real-address mode
PUSH OPERATIONSTORE VALUES.stack 1000h ;allocate 4096 bytes for the stack.datavar1 dword 0A5hvar2 dword 1var3 dword 2.codepush var1push var2push var3
PUSH OPERATIONSTORE VALUES§Push var1 (000000A5) on a stack that alreadycontains one value (00000006)§A 32-bit push operation decrements the stackpointer by 4 and copies a value into thelocation in the stack pointed to by the stackpointer.
PUSH OPERATIONSTORE VALUES§After, push var2 and push var3The stack grows downward. The area below ESP is alwaysavailable (unless the stack has overflowed).
POP OPERATIONRETRIEVE VALUES§A pop operation removes a value from the stack.§The popped value copied to the operand from thestack.§ESP is incremented (by the stack element size) topoint to the next-highest location in the stack.
POP OPERATIONRETRIEVE VALUESsum dword ?pop sumpop eax
POP OPERATIONRETRIEVE VALUESpop eax§The area of the stack below ESP is logically empty,and will be overwritten the next time the currentprogram executes any instruction that pushes avalue on the stack.
PUSHANDPOP INSTRUCTIONS¢The PUSH instruction first decrements ESP and thencopies a source operand into the stack.¢A 16-bit operand causes ESP to be decremented by 2.A 32-bit operand causes ESP to be decremented by 4.¢PUSH syntax:PUSHr/m16PUSHr/m32PUSHimm32
PUSHANDPOP INSTRUCTIONS¢The POP instruction first copies the contents ofthe stack element pointed to by ESP into a 16- or32-bit destination operand and then incrementsESP.¢If the operand is 16 bits, ESP is incremented by 2;if the operand is 32 bits, ESP is incremented by 4:¢POP syntax:POPr/m16POPr/m32
USINGPUSHANDPOP¢Save and restore registers when they containimportant values. PUSH and POP instructionsoccur in the opposite order.¢INCLUDE Irvine32.inc•The DumpMem procedure writes a range of memory tothe console window in hexadecimal.•Pass it the starting address in ESI, the number of unitsin ECX, and the unit size in EBX (1 = byte, 2 = word, 4 =doubleword).
INCLUDE Irvine32.inc.datadwordVal dword 1, 2, 3, 5, 9, 11.codemain PROCpush esi; push registerspush ecxpush ebxmovesi,OFFSET dwordVal; display some memorymov ecx,LENGTHOF dwordValmov ebx,TYPE dwordValcall DumpMempopebx; restore registerspopecxpopesiexitmain ENDPEND main
USINGPUSHANDPOP

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 82 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
tony chern
Tags
Call stack, ESI, Stack Operations

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture