The Stack - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

The Stack

Description:

The stack is an area in memory that its purpose is to provide a space for ... data contains variables data declarations, .text contains a code. ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 16
Provided by: csBg
Category:
Tags: area | code | list | stack

less

Transcript and Presenter's Notes

Title: The Stack


1
The Stack
  • The stack is an area in memory that its purpose
    is to provide a space for temporary storage of
    addresses and data items.
  • Every cell in the stack is in size of 2/4 bytes.
  • The register SS holds the address of the stack
    the beginning of the area in memory.
  • The registers SP/ESP holds the address of the
    next available free cell in the stack . i.e a
    pointer to the top of the stack.
  • If the address mode is 32 bit, we use ESP as
    pointer and every cell is 4 bytes. If the address
    mode is 16 bit, we use SP as stack pointer and
    every cell is 2 bytes.
  • Example for 16-bit mode stack in size of 216
    bits.

SS 0000
2 bytes
SP FFFDh
5
3
FFFFh
2
Stack Operations
  • PUSH
  • This instruction push data on stack. It
    decrements the stack pointer (SP or ESP) by 2 or
    4, and then stores the given value at SSSP or
    SSESP.
  • (SS is the stack segment address, ESP/SP is
    the stack pointer address).
  • The data pushed into the highest address of
    the stack, in little endian order.
  • The size of the operand determine which
    register to use as stack pointer and whether the
    stack pointer is decremented by 2 or 4.
  • Example
  • push the content of ax to stack in little
    endian order
  • move ax, 12AFh
  • push ax

SP
AF12
3
  • PUSHAx
  • This instruction Push All General-Purpose
    Registers.
  • PUSHAW pushes, in succession, AX, CX, DX, BX, SP,
    BP, SI and DI on the stack, decrementing the
    stack pointer by a total of 16.
  • PUSHAD pushes, in succession, EAX, ECX, EDX, EBX,
    ESP, EBP, ESI and EDI on the stack, decrementing
    the stack pointer by a total of 32.
  • In both cases, the value of SP or ESP pushed
    is its original value, as it had before the
    instruction was executed.
  • PUSHA is an alias mnemonic for either PUSHAW or
    PUSHAD, depending on the current BITS setting.
  • Note that the registers are pushed in order
    of their numeric values in opcodes.
  • PUSHFx
  • PUSHFW pushes the bottom 16 bits of the flags
    register (or the whole flags register, on
    processors below a 386) onto the stack.
  • PUSHFD pushes the entire flags register onto the
    stack.
  • PUSHF is an alias for either PUSHFW or PUSHFD,
    depending on the current BITS setting (16-bits
    mode or 32-bits mode).

4
  • POP
  • POP loads a value from the stack (from
    SSSP or SSESP) and then increments the
    stack pointer (by 2 or 4).
  • The size of the operand determine which
    register to use as stack pointer and whether the
    stack pointer is incremented by 2 or 4.
  • Example
  • mov ax, 3
  • push ax
  • mov bx,12AFh
  • push bx
  • pop ax ax 12AFh
  • pop bx bx 3

5
  • POPAx
  • Pop All General-Purpose Registers.
  • POPAW pops a word from the stack into each of,
    successively, DI, SI, BP, nothing (it discards a
    word from the stack which was a placeholder for
    SP), BX, DX, CX and AX. It is intended to reverse
    the operation of PUSHAW, but it ignores the value
    for SP that was pushed on the stack by PUSHAW.
  • POPAD pops twice as much data, and places the
    results in EDI, ESI, EBP, nothing (placeholder
    for ESP), EBX, EDX, ECX and EAX. It reverses the
    operation of PUSHAD.
  • POPA is an alias for either POPAW or POPAD,
    depending on the current BITS setting.
  • POPFx
  • Pop Flags Register.
  • POPFW pops a word from the stack and stores it in
    the bottom 16 bits of the flags register (or the
    whole flags register, on processors below a 386).
  • POPFD pops a doubleword and stores it in the
    entire flags register.
  • POPF is an alias for either POPFW or POPFD,
    depending on the current BITS setting.

6
Running NASM - reminder
To assemble a file, you issue a command of the
form gt nasm -f ltformatgt ltfilenamegt -o
ltoutputgt Example gt nasm -f elf mytry.s -o
myelf.o It would create myelf.o file that has elf
format (executable and linkable format).We use
main.c file (that is written in C language) to
start our program, and sometimes also for input /
output from a user. So to compile main.c with our
assembly file we should execute the following
command gt gcc main.c myelf.o -o myexe.out It
would create executable file myexe.out.In order
to run it you should write its name on the
command line gt myexe.out
7
Producing a listing file
  • If you supply the -l option to NASM, followed
    (with the usual optional space) by a file name,
    NASM will generate a source-listing file for you,
    in which addresses and generated code are listed
    on the left, and the actual source code, with
    expansions of multi-line macros
  • For example
  • nasm -f elf asm0.s -l asm.lst

8
Example of a listing file
  • 1 section
    .rodata
  • 2 LC0
  • 3 00000000 46726F6D2061736D2E- DB
    "From asm.s the result is d", 10, 0
  • 4 00000009 733A20746865207265-
  • 5 00000012 73756C742069733A20-
  • 6 0000001B 2025640A00
  • 7 section
    .text
  • 8
    align 16
  • 9
    global my_func
  • 10
    extern printf
  • 11 my_func
  • 12 00000000 55
    push ebp
  • 13 00000001 89E5
    mov ebp, esp
  • 14
  • 15 00000003 8B5D0C
    mov ebx, dword ebp12 get m
  • 16 00000006 8B4D08
    mov ecx, dword ebp8 get n
  • 17 00000009 B800000000
    mov eax, 0
  • 18 my_loop
  • 19 0000000E 01D8
    add eax, ebx

9
hello.s
section .data hello db 'Hello world!',10
'Hello world!' plus a linefeed
character helloLen equ 13
Length of the 'Hello world!' string
section .text global
_start _start mov eax,0 add eax,4
The system call for write (sys_write) mov
ebx,2 sub ebx,1 File descriptor 1 -
standard output mov ecx,hello Put the
offset of hello in ecx mov edx,helloLen
helloLen is a constant, so we don't need to say
mov edx,helloLen to get
it's actual value int 80h Call
the kernel mov eax,1 The system
call for exit (sys_exit) mov ebx,0
Exit with return code of 0 (no error) int 80h
  • Has two sections . data .text. .data contains
    variables data declarations, .text contains a
    code.
  • equ is used to define a constant, just like
    define in C.

10
main0.c file
include ltstdio.hgt define BUFFER_SIZE (128) ext
ern int my_func(char buf) int main(int argc,
char argv) char bufBUFFER_SIZE
printf("Enter string ") fflush(stdout)
fgets(buf, BUFFER_SIZE, stdin) printf("Got
string s\n", buf) my_func(buf) return
0
1. Include stdio.h library (standard I/O) 2. Get
online parameters (from the user) 3. Call to an
assembly function my_func Note It can contain
other functions that we can call from an assembly
file
11
Task1.s file
section .rodata LC0 DB "The result is s",
10, 0 section .text align 16 global
my_func extern printf my_func push ebp mov eb
p, esp push ecx mov ecx, dword ebp8
Your code should be here... push ecx push LC0
call printf add esp, 8 pop ecx mov esp,
ebp pop ebp ret
1. Has two sections .rodata .text.rodata
contains all read-only data declarations, .text
contains a code 2. Align 16 means that all data
and instruction should be at an address that is
divided by 16 (bits) (in another words, an
address should be even) 3. my_func is a function
that we define it should be global so that
main.c file would be able to call to it 4. We use
printf function of stdlib.h C library so we
should declare it as an external function because
it isnt defined in our assembly file (if we use
any function that is not in our file, we should
declare it as external) 5. The purpose of my_func
is to call printf with the string it got from
main as an argument.
12
The structure of programs in the tasks
section .rodata LC0 DB "The result is s",
10, 0 Format string section .text align
16 global my_func extern printf my_func push
ebp mov ebp, esp Entry code - set up ebp and
esp push ecx Save registers used in
function mov ecx, dword ebp8 Get argument
(pointer to string) Your code should be
here... push ecx Call printf with 2
arguments pointer to str push LC0 and
pointer to format string. call printf add
esp, 8 Clean up stack after call pop ecx
Restore registers used in this function mov esp,
ebp Function exit code pop ebp ret
13
Task 1
  • In this task we would use task1.s assembly file
    and main1.c C file.
  • Write a program that gets a string and convert
    numbers 0-9 to
  • letters A-J.

Task 2
In this task we would use task2.s assembly file
and main2.c C file. Write a program that gets a
string and prints it in hexadecimal
14
Parameters passing
  • To see an example for the C calling convention
    and parameters passing example, take the C
    program main0.c and assembly it as follows
  • gcc S main0.c
  • Observe parameters pass in C, compare to material
    given in class.

15
Task 3
  • Practice parameters passing from assembly to C
    and vice versa.
  • write a c program that performs
  • Takes a string from the user.
  • Call to C function length with the string as
    parameter.
  • Assembly function find_length performs
  • Call to length function (defined before) to
    find the strings length.
  • Call to printf to print the result.
  • C function length performs
  • Gets 1 parameters string.
  • Compute and return the length of the string.
Write a Comment
User Comments (0)
About PowerShow.com