You are on page 1of 15

CS 220

Feb 19, 2007


Program Layout
• .data(.rodata), .bss, .text
.data
• Directive Data Type
.ascii Text string
.asciz Null-terminated text string
.byte Byte value
.double Double-precision floating-point number
.float Single-precision floating-point number
.int 32-bit integer number
.long 32-bit integer number (same as .int)
.octa 16-byte integer number
.quad 8-byte integer number
.short 16-bit integer number
.single Single-precision floating-point number (same as .float)

.fill reserve buffer space


Example
.section .data
msg:
.ascii “This is a test message”
factors:
.double 37.45, 45.33, 12.30
height:
.int 54
length:
.int 62, 35, 47
Initialized with values
.bss
• Directive Description
.comm Declares a common memory area for data that is
not initialized to a value
.lcomm Declares a local common memory area for data that
is not initialized to a value

Zero-Filled
• Example
static int a;
int b;

.local a
.comm a,4,4
.comm b,4,4
Memory Arguments
.section . data
firstint:
.int 40
.section .text
.globl main
.type main, @function
main: Literal: $
pushl %ebp Register: %
movl %esp, %ebp
subl $24, %esp Memory: (no prefix)
andl $-16, %esp
Address: $label
movl $20, %eax
addl firstint, %eax

leave
ret

http://csserver.evansville.edu/~richardson/courses/CS220/resources/supplements/gas/instruction_set.html
Revisit Flags
• Status Flags
Flag Bit Name
CF 0 Carry flag
PF 2 Parity flag
AF 4 Adjust flag
ZF 6 Zero flag
SF 7 Sign flag
OF 11 Overflow flag
• Control Flag Flag manipulation:
DF=0 increase EIP(default), DF=1 decrease EIP stc
• System Flags clc
std
IF bit 9 Interrupt enable flag
cld
……
sti
cli
Revisit Flags
• Meanings of the OF, CF, SF, and ZF Flags
• The following table describes the meanings of the four
flags used in conditional branching:
• OF (Overflow)
– 1 -- result is outside signed-number range0 -- otherwise
• CF (Carry)
– Carry out of (borrow into) high-order bit.
– 1 -- result is outside unsigned-number range0 -- otherwise
• SF (Sign)
– High-order bit of result.
– 1 -- negative signed number.0 -- positive signed number.
• ZF (Zero)
– 1 -- result = 00 -- otherwise
Label & Jump
• Jmp location

Address offset:
Short jump (< 128 bytes)
Far jump (cross segment)
Near jump (all other)
Conditional Branches
• J?? location
Example:
JZ newlocation
• no far jumps – can’t cross segments
Conditional Branches
• Conditional Jumps
• The following table lists the most common jump instructions and the tests they perform:

• Instruction Jump Condition Test


• JE Jump if Equal ZF=1
• JNE Jump if Not Equal ZF=0
• JG Jump if Greater (ZF=0) AND (SF=OF)
• JGE Jump if Greater or Equal SF=OF
• JL Jump if Less SF≠OF
• JLE Jump if Less or Equal (ZF=1) OR (SF≠OF)

• The following conditional branches are similar to the above but involve comparisons which treat the operands as unsigned
integers:

• Instruction Jump Condition Test


• JA Jump if Above (CF=0) AND (ZF=0)
• JAE Jump if Above or Equal CF=0
• JB Jump if Below CF=1
• JBE Jump if Below or Equal (CF=1) OR (ZF=1)

• Finally, the branches below specifically test flags:

• Instruction Jump Condition Test


• JO Jump on Overflow OF=1
• JNO Jump on No Overflow OF=0
• JC Jump on Carry CF=1
• JNC Jump on No Carry CF=0
• JS Jump on Sign (Negative) SF=1
• JNS Jump on No Sign (Positive) SF=0
• JZ Jump if Zero (same as JE) ZF=1
• JNZ Jump if Not Zero ZF=0
Example 1
movl $15, %eax
movl $10, %ebx
cmp %eax, %ebx
jge greater
movl $1, %eax
greater:
movl $2, %eax
Example 2
movl $10, %edi
loop1:
#<other code instructions>
dec %edi
jz out
jmp loop1
out:
Example 3
movl $4, %ebx
subl $3, %ebx
jp overhere
movl $1, %eax
overhere:
movl $2, %eax
Loops
• Instruction Description
LOOP Loop until the ECX register is zero
LOOPE/LOOPZ Loop until either the ECX register is zero, or the ZF flag is not set
LOOPNE/LOOPNZ Loop until either the ECX register is zero, or the ZF flag is set

< code before the loop >


movl $100, %ecx
label1:
< code to loop through >
loop label1
< code after the loop >

• Only Short Jump can be performed (within 8-bit offset)


• Decreasing ECX without affecting ZF

JCXZ Jump if CX register is 0


JECXZ Jump if ECX register is 0

You might also like