Transcript mov

Lecture
Topic
When ?
1
Introduction to C Programming in Unix
Environment - I
October 20, 2013
2
Introduction to C Programming in Unix
Environment - II
October 27, 2013
3
Introduction to Assembly
November 3, 2013
4
Functions and System Calls (Assembly)
November 17, 2013
November 10, 2013
Midterm A ( December 4, 2013)
5
Unix Processes
December 8, 2013
6
Programs Execution
December 15, 2013
7
Introduction to script languages (Python)
December 22, 2013
8
Web programming
January 5, 2014
Midterm B (January 15, 2014)
2

Pentium has 10 32-bit and 6 16-bit registers

Registers are grouped into:
 General registers
 Control registers
 Segment registers

General registers
 Data registers
 Pointer registers
 Index registers
Abed Asi - ESPL
3

Jump if the specified condition is satisfied
j<cond>

label
;identifies the condition
The condition being tested is the result of the last arithmetic or logic
operation
read_char:
mov
. . .
(code
. . .
cmp
je
inc
jmp
DL,0
for reading a character into AL)
AL,0DH
CR_received
CL
read_char
but, the CMP
doesn’t save the
result, so what
really happens ?!!
;compares the character to CR
; if equal, jump to CR_received
;otherwise, increment CL and
; go back to read another char.
CR_received:
mov
DL, AL
Abed Asi - ESPL
4
mov
CL,50
repeat1:
<loop body>
dec
CL
jnz
repeat1
. . .
. . .
Abed Asi - ESPL
mov
ECX,50
repeat1:
<loop body>
loop repeat1
. . .
. . .
5

Functions and the Stack
 Pentium Implementation of the stack
 Uses of the stack
 Calling Functions
Abed Asi - ESPL
6

A stack is a last-in-first-out (LIFO) data structure

The top-of-the-stack (TOS) is indicated by ESP register

The key characteristics:
 Only words (16-bit) or doublewords (32-bit) are saved on the stack
 The stack grows toward lower memory address (downward)
 TOS always points to the last inserted data item
 TOS points to the lower byte of the last inserted word
Abed Asi - ESPL
7
Abed Asi - ESPL
8
push source
pop destination

The operands can be a 16-bit or 32-bit general purpose
registers, or a word or a doubleword in memory
Abed Asi - ESPL
9
push 21ABH
push 7FBD329AH
Abed Asi - ESPL
pop EBX
10
Abed Asi - ESPL
11

The stack is used for three main purposes
 Temporary Storage of Data
 Transfer of Control
 Parameter Passing
Abed Asi - ESPL
12



Abed Asi - ESPL
value1 and value2 are in memory
We want to exchange their values
mov doesn’t work, why ?
13

The Pentium provides call and ret
instructions

After the call instruction, the EIP points
to the next instruction to be executed
High
<return address >
The processor pushes the content of the
EIP (of the calling function) onto the
stack
ESP = ESP – 4
ESP = EIP
call proc-name
EIP = EIP + d

Low
Abed Asi - ESPL
14

The ret instruction is used to transfer
control from the called procedure to
the calling procedure
ret

EIP = ESP
ESP = ESP + 4
High
<return address>
Note: integral return value of
procedures are stored in EAX
Low
Abed Asi - ESPL
15

It is more complicated than that used in high-level languages

The calling procedure first places all the parameters need by
the called procedure in the stack
For example, consider passing two 16-bit
parameters to a SUM procedure
push number1
push number2
call sum
Abed Asi - ESPL
16

So, how do we retrieve the parameters now ?

Since the stack is a sequence of memory location ESP+4
points to number2, and ESP+6 to number1

For instance, to read number2 we can invoke:
mov
EBX, [ESP+4]
Are we done ? What type of
problems we would
encounter?
Abed Asi - ESPL
17

The stack pointer is updated by the push and pop instructions
 the relative offset changes

A better alternative is to use the EBP register
mov EBP, ESP
mov AX, [EBP+4]

Done?
Since every procedure uses the EBP register, it should be preserved
push EBP
mov EBP, ESP
mov AX, [EBP+4]
Abed Asi - ESPL
18
sum:
push number1
push number2
call sum
Abed Asi - ESPL
push EBP
mov EBP, ESP
<SUM CODE>
mov ESP, EBP
pop EBP
ret
19
section .DATA
string db “ESPL”,0
section .CODE
mov EAX, string
push EAX
inc EAX
push EAX
call swap
swap:
Abed Asi - ESPL
push EBP
mov EBP, ESP
push EBX
mov EBX, [EBP+12]
xchg AL, [EBX]
mov EBX, [EBP+8]
xchg AL, [EBX]
mov EBX, [EBP+12]
xchg AL, [EBX]
pop EBX
mov ESP, EBP
pop EBP
ret
;EAX = string[0] pointer
;EAX = string[1] pointer
;save EBX – procedure uses EBX
; EBX = first character pointer
; swap between operands
; EBX = second character pointer
; EBX = first character pointer
20
func:
push EBP
mov EBP, ESP
sub ESP, 8
...
Abed Asi - ESPL
21
Abed Asi - ESPL
22