introduction to assembly language ia32-iii
DESCRIPTION
Introduction to Assembly Language IA32-III. Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University. Macro’s. The commands .macro and . endm allow you to define macros that generate assembly output - PowerPoint PPT PresentationTRANSCRIPT
Introduction to Assembly Language IA32-III
Summer 2014
COMP 2130 Intro Computer Systems
Computing ScienceThompson Rivers University
Introduction 2
Macro’s
The commands .macro and .endm allow you to define macros that generate assembly output
It may allow to pass the parameters too separated by comma or space
.macro sum a b
movl \a, %eax
mov \b, %ebx
addl %ebx, %eax
.endm
TRU-COMP2130
Introduction 3
Sample The macros may be used as:
.macro write str str_size
movl $4, %eax
movl $1, %ebx
movl \str, %ecx
movl \str_size, %edx
int $0x80
.endm
.macro writenum str
movl $4, %eax
movl $1, %ebx
movl \str, %ecx
movl $1, %edx
int $0x80
.endm
.macro read num
movl $3, %eax
movl $0, %ebx
movl \num, %ecx
movl $2, %edx
int $0x80
.endmTRU-COMP2130
write $prompt_str1, $STR1_SIZEread $input1
write $prompt_str2, $STR2_SIZEread $input2
movl input1, %eaxsubl $0x30, %eax
mov input2, %ebxsubl $0x30, %ebx
addl %ebx, %eaxaddl $0x30, %eaxmovl %eax, answrite $prompt_str3, $STR3_SIZEwritenum $ans
Condition Codes (Explicit Setting: Compare)
Explicit Setting by Compare Instruction cmpl Src2, Src1 cmpl b,a like computing a-b without setting destination,
i.e., compare a to b
ZF set if (a == b) SF set if (a - b) < 0 (as signed, i.e., a < b) Jump instructions use these flags for controlling program execution, i.e.,
conditional branching.
CF set if carry out from most significant bit (used for unsigned comparisons)
OF set if two’s-complement (signed) overflow(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)
Carnegie Mellon
Condition Codes (Explicit Setting: Test)
Explicit Setting by Test instruction testl Src2, Src1
testl b,a like computing a&b without setting destination
Sets condition codes based on value of Src1 & Src2 Useful to have one of the operands be a mask
ZF set when a&b == 0 SF set when a&b < 0
Carnegie Mellon
Conditional Branch Example
int absdiff(int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}
int absdiff(int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
Carnegie Mellon
Can you rewrite the above code using goto statements?
Can you write C code?
xy
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
C allows “goto” as means of transferring control
Closer to machine-level programming style
Generally considered bad coding style in high-level programming.
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
Carnegie Mellon
xy
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
Carnegie Mellon
xy
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
Carnegie Mellon
xy
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y; goto Exit;Else: result = y-x;Exit: return result;}
absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L6subl %eax, %edxmovl %edx, %eaxjmp .L7
.L6:subl %edx, %eax
.L7:popl %ebpret
Body1
Setup
Finish
Body2b
Body2a
Carnegie Mellon
xy
C Codeint pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}
int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}
Goto Version (if (…) goto …)int pcount_do(unsigned x){ int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}
int pcount_do(unsigned x){ int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}
“Do-While” Loop Example
Count number of 1’s in argument x (“popcount”) Use conditional branch to either continue looping or to exit loop
Carnegie Mellon
Goto Version (if (…) goto …)
“Do-While” Loop Compilation
Registers:%edx x%ecx result
movl $0, %ecx # result = 0.L2: # loop:
movl %edx, %eaxandl $1, %eax # t = x & 1addl %eax, %ecx # result += tshrl %edx # x >>= 1jne .L2 # If !0, goto loop
int pcount_do(unsigned x) { int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}
int pcount_do(unsigned x) { int result = 0;loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result;}
Carnegie Mellon
Non-goto Version (if (…) goto …)int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}
int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result;}
C Code
do Body while (Test);
do Body while (Test);
Goto Version (if (…) goto …)loop: Body if (Test) goto loop
loop: Body if (Test) goto loop
General “Do-While” Translation
Body:
Test returns integer = 0 interpreted as false ≠ 0 interpreted as true
{ Statement1; Statement2; … Statementn;}
Carnegie Mellon
C Code Goto Version (if (…) goto …)
“While” Loop Example
Is this code equivalent to the do-while version? Must jump out of loop if test fails
int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result;}
int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result;}
int pcount_do(unsigned x) { int result = 0; if (!x) goto done;loop: result += x & 0x1; x >>= 1; if (x) goto loop;done: return result;}
int pcount_do(unsigned x) { int result = 0; if (!x) goto done;loop: result += x & 0x1; x >>= 1; if (x) goto loop;done: return result;}
Carnegie Mellon
While version
while (Test) Bodywhile (Test) Body
Do-While Version
if (!Test) goto done; do Body while(Test);done:
if (!Test) goto done; do Body while(Test);done:
General “While” Translation
Goto Version
if (!Test) goto done;loop: Body if (Test) goto loop;done:
if (!Test) goto done;loop: Body if (Test) goto loop;done:
Carnegie Mellon
C Code
“For” Loop Example
Is this code equivalent to other versions?
#define WSIZE sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}
#define WSIZE sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}
Carnegie Mellon
“For” Loop Form
for (Init; Test; Update )
Body
General Form
for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; }
for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; }
i = 0i = 0
i < WSIZEi < WSIZE
i++i++
{ unsigned mask = 1 << i; result += (x & mask) != 0;}
{ unsigned mask = 1 << i; result += (x & mask) != 0;}
Init
Test
Update
Body
Carnegie Mellon
“For” Loop While Loop
for (Init; Test; Update )
Body
For Version
Init;
while (Test ) {
Body
Update;
}
While Version
Carnegie Mellon
“For” Loop … Goto
for (Init; Test; Update )
Body
For Version
Init;
while (Test ) {
Body
Update;
}
While Version
Init; if (!Test) goto done; do Body Update while(Test);done:
Init; if (!Test) goto done; do Body Update while(Test);done:
Init; if (!Test) goto done;loop: Body Update if (Test) goto loop;done:
Init; if (!Test) goto done;loop: Body Update if (Test) goto loop;done:
Carnegie Mellon
C Code
“For” Loop Conversion Example
#define WSIZE sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}
#define WSIZE sizeof(int)int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result;}
Goto Version
int pcount_for_gt(unsigned x) { int i; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; if (i < WSIZE) goto loop; done: return result;}
int pcount_for_gt(unsigned x) { int i; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; if (i < WSIZE) goto loop; done: return result;}
Init
!Test
Body
UpdateTest
Carnegie Mellon
Introduction 28
Example 2
.section .data
prompt_str1:
.ascii "Enter first number: "
str1_end:
.set STR1_SIZE, str1_end-prompt_str1
prompt_str2:
.ascii "\nThe number entered is : "
str2_end:
.set STR2_SIZE, str2_end-prompt_str2
.section .bss
.lcomm input1 1
.section .text
.globl _start
_start:
movl $4, %eax
movl $1, %ebx
movl $prompt_str1, %ecx
movl $STR1_SIZE, %edx
int $0x80
movl $3, %eax
movl $0, %ebx
movl $input1, %ecx
movl $2, %edx
int $0x80
movl $4, %eax
movl $1, %ebx
movl $prompt_str2, %ecx
movl $STR2_SIZE, %edx
int $0x80
movl $4, %eax
movl $1, %ebx
movl $input1, %ecx
movl $2, %edx
int $0x80
exit:
movl $1, %eax
movl $0, %ebx
int $0x80
TRU-COMP2130