1 if (a == b) 2 { 3 /* true stuff here */ 4 } 5 else 6 { 7 /* false stuff here */ 8 } 10 /* done either way (i.e., whether true.

Size: px
Start display at page:

Download "1 if (a == b) 2 { 3 /* true stuff here */ 4 } 5 else 6 { 7 /* false stuff here */ 8 } 10 /* done either way (i.e., whether true."

Transcription

1

2 1 movl a, %eax 2 movl b, %ebx 3 cmpl %eax, %ebx 4 je truestuff 5 6 /* false stuff here */ 7 jmp DoneEitherWay 8 9 truestuff: 10 /* if true code here */ 1 int absdiff(int x, int y) 3 if (x<y) 4 return y-x; 5 return x-y; 6 } DoneEitherWay: 13 /* done whether true or false */ 1 if (a == b) 3 /* true stuff here */ 4 } 5 else 6 { 7 /* false stuff here */ 8 } 9 10 /* done either way (i.e., whether true 11 or false) */

3 1.section.text 2.globl max 3.type 4 max: 5 pushl %ebp 6 movl %esp, %ebp 7 movl $0, %eax 8 movl 8(%ebp), %ecx 9 movl 12(%ebp), %edx 10 cmpl %ecx, %edx 11 jge L1 12 movl %ecx, %eax 13 jmp done 14 L1: 15 movl %edx, %eax 16 done: 17 movl %ebp, %esp 18 popl %ebp 19 ret 1.section.text 2.globl max 3.type 4 max: 5 pushl %ebp 6 movl %esp, %ebp 7 movl $0, %eax 8 movl 8(%ebp), %ecx 9 movl 12(%ebp), %edx 10 cmpl %ecx, %edx 11 jge L1 12 movl %ecx, %eax 13 jmp done 14 L1: 15 movl %edx, %eax 16 done: 17 movl %ebp, %esp 18 popl %ebp 19 ret 1 int absdiff(int x, int y) 3 if (x<y) 4 return y-x; 5 return x-y; 6 } 1 int absdiff(int x, int y) 3 int rval; 4 if (x < y) 5 goto less; 6 rval = y-x; 7 goto done; 8 less: 9 rval = y - x; 10 done: 11 return rval; 12 } 1 int absdiff(int x, int y) 3 if (x<y) 4 return y-x; 5 return x-y; 6 } 1 int absdiff(int x, int y) 3 int rval; 4 if (x < y) 5 goto less; 6 rval = y-x; 7 goto done; 8 less: 9 rval = y - x; 10 done: 11 return rval; 12 } 1.file "absdiff.c" 2.text 3.globl absdiff 4.type 5 absdiff: 6 pushl %ebp 7 movl %esp, %ebp 8 subl $4, %esp 9 movl 8(%ebp), %eax 10 cmpl 12(%ebp), %eax 11 jge.l2 12 movl 8(%ebp), %edx 13 movl 12(%ebp), %eax 14 movl %eax, %ecx 15 subl %edx, %ecx 16 movl %ecx, -4(%ebp) 17 jmp.l3 18.L2: 19 movl 12(%ebp), %edx 20 movl 8(%ebp), %eax 21 movl %eax, %ecx 22 subl %edx, %ecx 23 movl %ecx, -4(%ebp) 24.L3: 25 movl -4(%ebp), %eax 26 leave 27 ret 28.size absdiff,.-absdiff 29.ident "GCC: (Ubuntu ubuntu12) 4 30.section.note.GNU-stack,"",@progbits

4 1 while (a < b) 3 /* loop body */ 4 } 1 while (a < b) 3 /* loop body */ 4 } 1 movl a, %eax 2 movl b, %ebx 3 loop_beginning: 4 cmpl %ebx, %eax 5 jge loop_ending 6 7 loop_body: 8 9 /* loop body */ jmp loop_beginning 12 loop_ending: 1.section.text 2.globl to_upper 3.type 1 int to_upper(int c) 4 to_upper: 5 pushl %ebp 3 if (c > 122 c < 97) 6 movl %esp, %ebp 4 return c; 7 xorl %eax, %eax 5 return c-32; 8 movl 8(%ebp), %eax 6 } 9 cmpl $122, %eax 10 jg done 11 cmpl $97, %eax 12 jl done 13 addl $-32, %eax 14 done: 15 movl %ebp, %esp 16 popl %ebp 17 ret

5 1.section.text 2.globl sum 3.type 4 sum: 5 pushl %ebp 6 movl %esp, %ebp 7 movl $0, %ecx 8 movl $0, %eax 9 movl 8(%ebp), %edx 10 begin: 11 cmpl 12(%ebp), %ecx 12 jge done 13 addl (%edx, %ecx, 4), %eax 14 incl %ecx 15 jmp begin 16 done: 17 movl %ebp, %esp 18 popl %ebp 19 ret 1 int rev(int A[], int len) { 2 int *p=a, 3 *q=a+len-1, 4 t1, t2; 5 6 while (p<q) { 7 t1=*p; 8 t2=*q; 9 10 *q=t1; 11 *p=t2; 12 p++; 13 q--; 14 } 15 } 1.section.text 2.globl rev_array 3.type 4 rev_array: 5 pushl %ebp 6 movl %esp, %ebp 7 pushl %ebx 8 movl 12(%ebp), %eax 9 decl %eax 10 movl 8(%ebp), %ecx 11 leal (%ecx,%eax,4), %edx 12 begin: 13 cmpl %ecx, %edx 14 jle done 15 movl (%ecx), %eax 16 movl (%edx), %ebx 17 movl %eax, (%edx) 18 movl %ebx, (%ecx) 19 addl $4, %ecx 20 addl $-4, %edx 21 jmp begin 22 done: 23 popl %ebx 24 movl %ebp, %esp 25 popl %ebp 26 ret 1.section.data 2.section.text 3.globl _start 4 _start: 5 pushl $3 #push second argument 6 pushl $2 #push first argument 7 call power #call the function 8 addl $8, %esp #move the stack pointer back 9 10 pushl %eax #save the first answer before 11 #calling the next function pushl $2 #push second argument 14 pushl $5 #push first argument 15 call power #call the function 16 addl $8, %esp #move the stack pointer back popl %ebx #The second answer is already 19 #in %eax. We saved the 20 #first answer onto the stack, 21 #so now we can just pop it 22 #out into %ebx 1.type 2 power: 3 pushl %ebp #save old base pointer 4 movl %esp, %ebp #make stack pointer the base pointer 5 subl $4, %esp #get room for our local storage 6 7 movl 8(%ebp), %ebx #put first argument in %ebx 8 movl 12(%ebp), %ecx #put second argument in %ecx 9 10 movl %ebx, -4(%ebp) #store current result power_loop_start: 13 cmpl $1, %ecx #if the power is 1, we are done 14 je end_power 15 movl -4(%ebp), %eax #move the current result into %eax 16 imull %ebx, %eax #multiply the current result by 17 #the base number 18 movl %eax, -4(%ebp) #store the current result decl %ecx #decrease the power 21 jmp power_loop_start #run for the next power addl %eax, %ebx #add them together 25 #the result is in %ebx movl $1, %eax #exit (%ebx is returned) 28 int $0x end_power: 24 movl -4(%ebp), %eax #return value goes in %eax 25 movl %ebp, %esp #restore the stack pointer 26 popl %ebp #restore the base pointer 27 ret

6 1 mov counter val into %ecx 2 lb: 3 do stuff 4 loop lb 1 /* file LoopInstrTst.s */ 2.section.data 3 msg: 4.asciz "ecx = %d\n" 5 6.section.text 7.equ LINUX_SYSCALL, 0x80 8.equ EXIT_SYSCALL, 1 9.globl _start 10 _start: 11 nop 12 movl $30, %ecx 13 loop_begin: 14 pushl %ecx 15 pushl %ecx 16 pushl $msg 17 call printf 18 addl $8, %esp 19 popl %ecx 20 loop loop_begin 21 /* loop instruction uses 22 ecx as a counter 23 don t need to decl ecx. */ movl $EXIT_SYSCALL, %eax 26 movl $0, %ebx 27 int $LINUX_SYSCALL 28 1.section.text 2.globl to_upper 3.type 1 int to_upper(int c) 4 to_upper: 5 pushl %ebp 3 if (c > 122 c < 97) 6 movl %esp, %ebp 4 return c; 7 xorl %eax, %eax 5 return c-32; 8 movl 8(%ebp), %eax 6 } 9 cmpl $122, %eax 10 jg done 11 cmpl $97, %eax 12 jl done 13 addl $-32, %eax 14 done: 15 movl %ebp, %esp 16 popl %ebp 17 ret 1.section.text 2.globl str_to_upper 3.type 1 void str_to_upper(char *str) { 4 str_to_upper: 2 char *s = str; 5 pushl %ebp 3 while (*s!= \0 ) { 6 movl %esp, %ebp 7 subl $4, %esp 4 *s = to_upper(*s); 8 movl 8(%ebp), %ecx 5 s++; 9 xorl %eax, %eax 6 } 10 begin: 7 } 11 movb (%ecx), %al 12 cmpb $0, %al 13 je done 14 movl %eax, -4(%ebp) 15 call to_upper 16 movb %al, (%ecx) 17 incl %ecx 18 jmp begin 19 done: 20 addl $4, %esp 21 movl %ebp, %esp 22 popl %ebp

7 1.type 2.globl strlen 3 4 strlen: 5 pushl %ebp 6 movl %esp, %ebp 7 8 /* use eax as counter */ 9 movl $0, %eax /* %edi now contains the address of the string */ 12 movl 8(%ebp), %edi 13 loop_beginning: 14 /* have we found the null character? */ 15 cmpb $0, (%edi) 16 je exit 17 /* increment counter */ 18 incl %eax 19 /* increment pointer */ 20 incl %edi 21 jmp loop_beginning 22 exit: 23 movl %ebp, %esp 24 popl %ebp 25 ret 1.type 2.globl strlen2 3 4 strlen2: 5 pushl %ebp 6 movl %esp, %ebp 7 8 /* %eax now contains the address 9 of the begining of the string */ 10 movl 8(%ebp), %eax 11 loop_beginning: 12 /* have we found the null character? */ 13 cmpb $0, (%eax) 14 je exit 15 /* increment counter */ 16 incl %eax 17 jmp loop_beginning 18 exit: 19 /* length = address of end of str 20 stored in eax - address of beginning 21 of the string stored in 8(%ebp) */ 22 subl 8(%ebp), %eax 23 movl %ebp, %esp 24 popl %ebp 25 ret 1 /* file LoopInstrTst.s */ 2.section.data 3 msg: 4.asciz "ecx = %d\n" 5 6.section.text 7.equ LINUX_SYSCALL, 0x80 8.equ EXIT_SYSCALL, 1 9.globl _start 10 _start: 11 nop 12 movl $30, %ecx 13 loop_begin: 14 pushl %ecx 15 pushl %ecx 16 pushl $msg 17 call printf 18 addl $8, %esp 19 popl %ecx 20 loop loop_begin 21 /* loop instruction uses 22 ecx as a counter 23 don t need to decl ecx. */ movl $EXIT_SYSCALL, %eax 26 movl $0, %ebx 27 int $LINUX_SYSCALL 28

8 cmpl Y, X je jump if they were equal jg jump if the 2nd was greater than the first jge jump if the 2nd was greater or equal than the first jl jump if the 2nd was less than the first jle jump if the 2nd was less than or equal to the first CF carry flag ZF zero flag SF sign flag OF overflow flag o o o o o

9 instruction synonym effect set condition sete D setz D ZF equal/zero setne D setnz D ZF not equal/not zero sets D D SF negative setns D D SF non-negative setg D setnle D (SF OF) & ZF greater (signed >) setge D setnl D (SF OF) greater or equal (signed >=) setl D setnge D SF OF less (signed <) setle D setng D (SF OF) ZF less or equal (signed <=) seta D setnbe D CF & ZF above (unsigned >) setae D setnb D CF above or equal (unsigned >=) setb D setnae D CF below (unsigned <) setbe D setna D CF ZF below or equal (unsigned (<=) instruction synonym jump condition description jmp label 1 direct jump jmp *operand 1 indirect jump je label jz ZF equal/zero jne label jnz ZF not equal/zero js label SF negative jns label SF non-negative jg label jnle (SF OF) & ZF greater (signed >) jge label jnl (SF OF) greater or equal (signed >=) jl label jnge SF OF less (signed <) jle label jng (SF OF) ZF less or equal (signed (<=) ja label jnbe CF & ZF above (unsigned >) jae label jnb CF above or equal (unsigned >=) jb label jnae CF below (unsigned <) jbe label jna CF ZF below or equal (unsigned <=) add sub,xor test cmp testx A,BA&B cmpx A,BB-A

10 cmpl %ebx,%eax jl %eax < %ebx %ebx < %eax 1 /* from Programming from the Ground Up */ 2.section.data 3 4 data_items: #These are the data items 5.long 3,67,34,222,45,75,54,34,44,33,22,11,66,0 6 7.section.text 8 9.globl _start 10 _start: 11 movl $0, %edi # move 0 into the index register 12 movl data_items(,%edi,4), %eax # load the first byte of data 13 movl %eax, %ebx # since this is the first item, %eax is 14 # the biggest start_loop: # start loop 17 cmpl $0, %eax # check to see if we ve hit the end 18 je loop_exit 19 incl %edi # load next value 20 movl data_items(,%edi,4), %eax 21 cmpl %ebx, %eax # compare values 22 jle start_loop # jump to loop beginning if the new 23 # one isn t bigger 24 movl %eax, %ebx # move the value as the largest 25 jmp start_loop # jump to loop beginning loop_exit: 28 # %ebx is the status code for the exit system call 29 # and it already has the maximum number 30 movl $1, %eax #1 is the exit() syscall 31 int $0x80 1 /* file altb.s */ 2.text 3 4.equ LINUX_SYSCALL, 0x80 5.equ EXIT_SYSCALL, globl _start 8 _start: 9 nop 10 movl $10, %eax 11 movl $20, %ebx 12 cmpl %ebx, %eax 13 setl %al 14 movzbl %al, %eax movl %eax, %ebx 17 movl $EXIT_SYSCALL, %eax 18 int $LINUX_SYSCALL 19

11 1 /* file SimpleArr.s */ 2.data 3 msg1: 4.asciz "A[%d]=%d\n" 5 msg2: 6.asciz "Done.\n" 7 8.text 9.globl _start 10.equ SIZE, _start: 12 pushl %ebp 13 movl %esp, %ebp /* add space on stack for 16 array of 10 ints */ 17 subl $40, %esp 18 leal (%esp), %ebx 19 movl $0, %edi 20 init_loop_beginning: 21 movl %edi, (%ebx, %edi, 4) 22 incl %edi 23 cmpl $SIZE, %edi 24 jl init_loop_beginning 25 movl $0, %edi 26 print_loop_beginning: 27 pushl %edi 28 pushl (%ebx, %edi, 4) 29 pushl $msg1 30 call printf 31 addl $12, %esp 32 incl %edi 33 cmpl $SIZE, %edi 34 jl print_loop_beginning pushl $msg2 37 call printf 38 addl $4, %esp /* we re exiting anyway, 41 but it can t hurt */ 42 movl %ebp, %esp 43 popl %ebp movl $1, %eax 46 movl $0, %ebx 47 int $0x80 1 #include <stdio.h> 2 3 #define NUMROWS 4 4 #define NUMCOLS int main(int argc, char **argv) 7 { 8 int A[NUMROWS][NUMCOLS], i, j; 9 10 printf("\n "); 11 for (i=0; i<numrows; i++) { 12 for (j=0; j<numcols; j++) 13 printf("%p ", (void*)&a[i][j]); 14 printf("\n "); 15 } return 0; 18 } 1 #include <stdio.h> 2 3 #define LEN 5 4 typedef struct { 5 char s[10]; 6 int i; 7 float f; 8 } junk; 9 10 int main(int argc, char **argv) 11 { 12 int i; 13 int inta[len]; 14 char chara[len]; 15 junk junka[len]; printf("inta[]\n"); 18 for (i=0; i<len; i++) 19 printf("%p ", (void*)&inta[i]); printf("\nchara[]\n"); 22 for (i=0; i<len; i++) 23 printf("%p ", (void*)&chara[i]); printf("\njunka[]\n"); 26 for (i=0; i<len; i++) 27 printf("%p ", (void*)&junka[i]); inta[0] inta[1] inta[2] inta[3] inta[4] 0xbffff4c8 0xbffff4cc 0xbffff4d0 0xbffff4d4 0xbffff4d8 chara[0] chara[1] chara[2] chara[3] chara[4] 0xbffff4c3 0xbffff4c4 0xbffff4c5 0xbffff4c6 0xbffff4c7 junka[0] junka[1] junka[2] junka[3] junka[4] 0xbffff45c 0xbffff470 0xbffff484 0xbffff498 0xbffff4ac printf("\n"); 30 return 0; 31 }

12 1 #include "car.h" 2 #include <stdio.h> 3 4 void printcar(car *c) 5 { 6 printf("make:%s\n", c->make); 7 printf("model:%s\n", c->model); 8 printf("year:%d\n", c->year); 9 printf("city MPG:%d\n", c->cmpg); 10 printf("highway MPG:%d\n", c->hmpg); 11 } void printcarlong(car *c) 14 { 15 printf("make[%p]:%s\n", (void*)&(c->make), c->make); 16 printf("model[%p]:%s\n", (void*)&(c->model), c->model); 17 printf("year[%p]:%d\n", (void*)&(c->year), c->year); 18 printf("city MPG[%p]:%d\n", (void*)&(c->cmpg), c->cmpg); 19 printf("highway MPG[%p]:%d\n", (void*)&(c->hmpg), c->hmpg); 20 } 1 #include "car.h" 2 3 int main(int argc, char **argv) 4 { 5 car c1 = {"Toyota", "Prius", 2004, 35, 50}; 6 car c2 = {"Ford", "Expedition", 2004, 8, 18}; 7 8 printcarlong(&c1); 9 printcarlong(&c2); 10 return 0; 11 } 12 0xbffff438 0xbffff43c 0xbffff440 0xbffff444 0xbffff448 0xbffff44c 0xbffff450 0xbffff454 0xbffff458 0xbffff45c 0xbffff460 0xbffff464 1 /* car.h */ 2 3 #ifndef CAR_H 4 #define CAR_H 5 6 typedef struct { 7 char make[15]; 8 char model[15]; 9 int year; 10 int cmpg; /* city MPG */ 11 int hmpg; /* highway MPG */ 12 } car; void printcar(car*); 15 void printcarlong(car*); #endif

13 1 make[0xbffff4b4]:toyota 2 model[0xbffff4c3]:prius 3 year[0xbffff4d4]: city MPG[0xbffff4d8]:35 5 highway MPG[0xbffff4dc]:50 6 make[0xbffff488]:ford 7 model[0xbffff497]:expedition 8 year[0xbffff4a8]: city MPG[0xbffff4ac]:8 10 highway MPG[0xbffff4b0]:18

14 size type lowest bits of address 1 char no restrictions 2 short lowest bit=0 2 4 int, float, pointer lowest two bits= double Windows - lowest 3 bits=000 2 Linux - lowest 2 bits=00 2

15 arch void somecfunc( ) { asm( assembly here ); /* OR */ asm ( assembly here ); } 1 /* file sizeoftst.c */ 2 #include <stdio.h> 3 4 typedef struct { 5 int x; 6 char c[5]; 7 } foo; 8 9 typedef struct { 10 int x; 11 char c; 12 } goo; 13 int main(int argc, char **argv) 14 { 15 foo F[10]; 16 goo G[10]; printf("sizeof foo = %lu\n", 19 sizeof(f[0])); 20 printf("sizeof F[] = %lu\n", 21 sizeof(f)); 22 printf("sizeof goo = %lu\n", 23 sizeof(g[0])); 24 printf("sizeof G[] = %lu\n", 25 sizeof(g)); 26 return 0; 27 } 1 /* file sizeoftst.c */ 2 #include <stdio.h> 3 4 typedef struct { 5 int x; 6 char c[5]; 7 } foo; 8 9 typedef struct { 10 int x; 11 char c; 12 } goo;

16 1 unsigned char c = ch; 2 3 if (c == \n ) 4 putchar( \r ); /* \n -> \r\n */ 5 6 /* int $0x10 is known to have bugs involving touching registers 7 it shouldn t. Be extra conservative... */ 8 asm volatile("pushal; pushw %%ds; int $0x10; popw %%ds; popal" 9 : : "b" (0x0007), "c" (0x0001), "a" (0x0e00 ch));

17 char description r use any general purpose register a %eax, %ax, or %al b %ebx, %bx, or %bl c %ecx, %cx, or %cl d %edx, %dx, or %dl S %esi D %edi m use memory, not a register 0, 1,..., 9 use some variable that you ve defined earlier in some list, e.g. asm( addl %0, %0 : a (var) : 0 (var)). In this case 0 refers to the first variable, var, in the output list.

18 char *getcpuid() { int func=0, bx, cx, dx; } char *cpuidstr; if ((cpuidstr = (char*)calloc(cpuid_strlen, 1))==NULL) return NULL; asm("cpuid" :"=b" (bx), "=c" (cx), "=d" (dx) : "a" (func)); memcpy(cpuidstr, &bx, 4); memcpy(&cpuidstr[4], &dx, 4); memcpy(&cpuidstr[8], &cx, 4); cpuidstr[cpuid_strlen-1]= \0 ; return cpuidstr; 1 /* file cpuid2.s */ 2.section.data 3 output: 4.asciz "The processor Vendor ID is %s \n" 5.section.bss 6.lcomm buffer, 12 7.section.text 8.globl _start 9 _start: 10 movl $0, %eax 11 cpuid 12 movl $buffer, %edi 13 movl %ebx, (%edi) 14 movl %edx, 4(%edi) 15 movl %ecx, 8(%edi) 16 pushl $buffer 17 pushl $output 18 call printf 19 addl $8, %esp 20 pushl $0 21 call exit

some things that you know in C assignment simple arithme7c opera7ons func7ons condi'onals loops

some things that you know in C assignment simple arithme7c opera7ons func7ons condi'onals loops chapter 3 part 3 1 some things that you know in C assignment simple arithme7c opera7ons func7ons condi'onals loops 2 Control Flow We know how to move data: between registers between registers and memory

More information

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16 Control Young W. Lim 2016-11-21 Mon Young W. Lim Control 2016-11-21 Mon 1 / 16 Outline 1 Introduction References Condition Code Accessing the Conditon Codes Jump Instructions Translating Conditional Branches

More information

Machine Programming 2: Control flow

Machine Programming 2: Control flow Machine Programming 2: Control flow CS61, Lecture 4 Prof. Stephen Chong September 13, 2011 Announcements Assignment 1 due today, 11:59pm Hand in at front during break or email it to cs61- staff@seas.harvard.edu

More information

The Hardware/Software Interface CSE351 Spring 2013

The Hardware/Software Interface CSE351 Spring 2013 The Hardware/Software Interface CSE351 Spring 2013 x86 Programming II 2 Today s Topics: control flow Condition codes Conditional and unconditional branches Loops 3 Conditionals and Control Flow A conditional

More information

Credits to Randy Bryant & Dave O Hallaron

Credits to Randy Bryant & Dave O Hallaron Mellon Machine Level Programming II: Arithmetic & Control Lecture 4, March 10, 2011 Alexandre David Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon 1 Today Complete addressing mode, address

More information

Machine-Level Programming II: Control and Arithmetic

Machine-Level Programming II: Control and Arithmetic Machine-Level Programming II: Control and Arithmetic CSCI 2400: Computer Architecture Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides 1 Today Complete addressing mode, address

More information

Sungkyunkwan University

Sungkyunkwan University - 2 - Complete addressing mode, address computation (leal) Arithmetic operations Control: Condition codes Conditional branches While loops - 3 - Most General Form D(Rb,Ri,S) Mem[ Reg[ R b ] + S Reg[ R

More information

CPS104 Recitation: Assembly Programming

CPS104 Recitation: Assembly Programming CPS104 Recitation: Assembly Programming Alexandru Duțu 1 Facts OS kernel and embedded software engineers use assembly for some parts of their code some OSes had their entire GUIs written in assembly in

More information

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung

ASSEMBLY II: CONTROL FLOW. Jo, Heeseung ASSEMBLY II: CONTROL FLOW Jo, Heeseung IA-32 PROCESSOR STATE Temporary data Location of runtime stack %eax %edx %ecx %ebx %esi %edi %esp %ebp General purpose registers Current stack top Current stack frame

More information

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes

Machine-Level Programming II: Arithmetic & Control. Complete Memory Addressing Modes Machine-Level Programming II: Arithmetic & Control CS-281: Introduction to Computer Systems Instructor: Thomas C. Bressoud 1 Complete Memory Addressing Modes Most General Form D(Rb,Ri,S)Mem[Reg[Rb]+S*Reg[Ri]+

More information

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

X86 Addressing Modes Chapter 3 Review: Instructions to Recognize X86 Addressing Modes Chapter 3" Review: Instructions to Recognize" 1 Arithmetic Instructions (1)! Two Operand Instructions" ADD Dest, Src Dest = Dest + Src SUB Dest, Src Dest = Dest - Src MUL Dest, Src

More information

Introduction to 8086 Assembly

Introduction to 8086 Assembly Introduction to 8086 Assembly Lecture 5 Jump, Conditional Jump, Looping, Compare instructions Labels and jumping (the jmp instruction) mov eax, 1 add eax, eax jmp label1 xor eax, eax label1: sub eax, 303

More information

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly II: Control Flow Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 Processor State %eax %edx Temporary data Location of runtime stack

More information

x64 Cheat Sheet Fall 2014

x64 Cheat Sheet Fall 2014 CS 33 Intro Computer Systems Doeppner x64 Cheat Sheet Fall 2014 1 x64 Registers x64 assembly code uses sixteen 64-bit registers. Additionally, the lower bytes of some of these registers may be accessed

More information

Process Layout and Function Calls

Process Layout and Function Calls Process Layout and Function Calls CS 6 Spring 07 / 8 Process Layout in Memory Stack grows towards decreasing addresses. is initialized at run-time. Heap grow towards increasing addresses. is initialized

More information

Machine Level Programming II: Arithmetic &Control

Machine Level Programming II: Arithmetic &Control Machine Level Programming II: Arithmetic &Control Arithmetic operations Control: Condition codes Conditional branches Loops Switch Kai Shen 1 2 Some Arithmetic Operations Two Operand Instructions: Format

More information

CS241 Computer Organization Spring Addresses & Pointers

CS241 Computer Organization Spring Addresses & Pointers CS241 Computer Organization Spring 2015 Addresses & Pointers 2-24 2015 Outline! Addresses & Pointers! leal - load effective address! Condition Codes & Jumps! conditional statements: if-then-else! conditional

More information

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 n Mello Machine-Level Programming II: Arithmetic & Control 15-213/18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 Instructors: Gregory Kesden The course that gives CMU its Zip! Last Time:

More information

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

1 /* file cpuid2.s */ 4.asciz The processor Vendor ID is %s \n 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start. 1 /* file cpuid2.s */ 2.section.data 3 output: 4.asciz "The processor Vendor ID is %s \n" 5.section.bss 6.lcomm buffer, 12 7.section.text 8.globl _start 9 _start: 10 movl $0, %eax 11 cpuid 12 movl $buffer,

More information

Assembly Language: IA-32 Instructions

Assembly Language: IA-32 Instructions Assembly Language: IA-32 Instructions 1 Goals of this Lecture Help you learn how to: Manipulate data of various sizes Leverage more sophisticated addressing modes Use condition codes and jumps to change

More information

Intel Instruction Set (gas)

Intel Instruction Set (gas) Intel Instruction Set (gas) These slides provide the gas format for a subset of the Intel processor instruction set, including: Operation Mnemonic Name of Operation Syntax Operation Examples Effect on

More information

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions? administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions? exam on Wednesday today s material not on the exam 1 Assembly Assembly is programming

More information

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation Homework 0: 2.84 Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation Homework 0: 2.84 Given: k-bit exponent, n-bit fraction 7.0: 0111 = 1.11 2

More information

System Programming and Computer Architecture (Fall 2009)

System Programming and Computer Architecture (Fall 2009) System Programming and Computer Architecture (Fall 2009) Recitation 2 October 8 th, 2009 Zaheer Chothia Email: zchothia@student.ethz.ch Web: http://n.ethz.ch/~zchothia/ Topics for Today Classroom Exercise

More information

Machine- Level Programming II: Arithme6c & Control

Machine- Level Programming II: Arithme6c & Control Machine- Level Programming II: Arithme6c & Control 15-213: Introduc0on to Computer Systems 5 th Lecture, Sep. 7, 2010 Instructors: Randy Bryant and Dave O Hallaron Modified by Karen L. Karavanic 2015 1

More information

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ

BAHAR DÖNEMİ MİKROİŞLEMCİLER LAB4 FÖYÜ LAB4 RELATED INSTRUCTIONS: Compare, division and jump instructions CMP REG, memory memory, REG REG, REG memory, immediate REG, immediate operand1 - operand2 Result is not stored anywhere, flags are set

More information

CSC 8400: Computer Systems. Machine-Level Representation of Programs

CSC 8400: Computer Systems. Machine-Level Representation of Programs CSC 8400: Computer Systems Machine-Level Representation of Programs Towards the Hardware High-level language (Java) High-level language (C) assembly language machine language (IA-32) 1 Compilation Stages

More information

CS61 Section Solutions 3

CS61 Section Solutions 3 CS61 Section Solutions 3 (Week of 10/1-10/5) 1. Assembly Operand Specifiers 2. Condition Codes 3. Jumps 4. Control Flow Loops 5. Procedure Calls 1. Assembly Operand Specifiers Q1 Operand Value %eax 0x104

More information

Process Layout, Function Calls, and the Heap

Process Layout, Function Calls, and the Heap Process Layout, Function Calls, and the Heap CS 6 Spring 20 Prof. Vern Paxson TAs: Devdatta Akhawe, Mobin Javed, Matthias Vallentin January 9, 20 / 5 2 / 5 Outline Process Layout Function Calls The Heap

More information

Machine-Level Programming II: Control Flow

Machine-Level Programming II: Control Flow Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures Fabián E. Bustamante, Spring 2010 Processor state (ia32, partial) Information about currently

More information

Machine- Level Programming II: Arithme c & Control

Machine- Level Programming II: Arithme c & Control Machine- Level Programming II: Arithme c & Control 15-213 / 18-213: Introduc on to Computer Systems 6 th Lecture, Sep. 12, 2013 Instructors: Randy Bryant, David O Hallaron, and Greg Kesden 1 Today Complete

More information

Chapter 3 Machine-Level Programming II Control Flow

Chapter 3 Machine-Level Programming II Control Flow Chapter 3 Machine-Level Programming II Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements Condition Codes Single Bit Registers CF Carry Flag

More information

Basic Assembly Instructions

Basic Assembly Instructions Basic Assembly Instructions Ned Nedialkov McMaster University Canada SE 3F03 January 2013 Outline Multiplication Division FLAGS register Branch Instructions If statements Loop instructions 2/21 Multiplication

More information

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016 CS 31: Intro to Systems ISAs and Assembly Kevin Webb Swarthmore College February 9, 2016 Reading Quiz Overview How to directly interact with hardware Instruction set architecture (ISA) Interface between

More information

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM EXPERIMENT WRITE UP AIM: Assembly language program to search a number in given array. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

More information

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018 CS 31: Intro to Systems ISAs and Assembly Kevin Webb Swarthmore College September 25, 2018 Overview How to directly interact with hardware Instruction set architecture (ISA) Interface between programmer

More information

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs CSC 2400: Computer Systems Towards the Hardware: Machine-Level Representation of Programs Towards the Hardware High-level language (Java) High-level language (C) assembly language machine language (IA-32)

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 2 nd Edition by Bryant and O'Hallaron

More information

Machine- Level Programming II: Arithme6c & Control

Machine- Level Programming II: Arithme6c & Control Machine- Level Programming II: Arithme6c & Control Computer Architecture Instructor: Norbert Lu*enberger based on the book by Randy Bryant and Dave O Hallaron 1 Today Complete addressing mode, address

More information

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS

LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS LABORATORY WORK NO. 7 FLOW CONTROL INSTRUCTIONS 1. Object of laboratory The x86 microprocessor family has a large variety of instructions that allow instruction flow control. We have 4 categories: jump,

More information

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G CS 33: Week 3 Discussion x86 Assembly (v1.0) Section 1G Announcements - HW2 due Sunday - MT1 this Thursday! - Lab2 out Info Name: Eric Kim (Section 1G, 2-4 PM, BH 5419) Office Hours (Boelter 2432) - Wed

More information

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature CSE2421 FINAL EXAM SPRING 2013 Name KEY Instructions: This is a closed-book, closed-notes, closed-neighbor exam. Only a writing utensil is needed for this exam. No calculators allowed. If you need to go

More information

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut mith College Computer Science CSC231 Assembly Week #11 Fall 2017 Dominique Thiébaut dthiebaut@smith.edu Back to Conditional Jumps Review sub eax, 10 jz there xxx xxx there:yyy yyy Review cmp eax, 10 jz

More information

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut

mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut mith College Computer Science CSC231 Assembly Week #9 Spring 2017 Dominique Thiébaut dthiebaut@smith.edu 2 Videos to Watch at a Later Time https://www.youtube.com/watch?v=fdmzngwchdk https://www.youtube.com/watch?v=k2iz1qsx4cm

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 2 nd Edition by Bryant and O'Hallaron

More information

CS356 Unit 5. Translation to Assembly. Translating HLL to Assembly ASSEMBLY TRANSLATION EXAMPLE. x86 Control Flow

CS356 Unit 5. Translation to Assembly. Translating HLL to Assembly ASSEMBLY TRANSLATION EXAMPLE. x86 Control Flow 5.1 5.2 CS356 Unit 5 x86 Control Flow Compiler output ASSEMBLY TRANSLATION EXAMPLE Translation to Assembly 5.3 Translating HLL to Assembly 5.4 We will now see some C code and its assembly translation A

More information

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly II: Control Flow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly II: Control Flow Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Processor State (x86-64) RAX 63 31 EAX 0 RBX EBX RCX RDX ECX EDX General-purpose

More information

Condition Codes The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics

Condition Codes The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics 15-213 The course that gives CMU its Zip! Machine-Level Programming II Control Flow Sept. 13, 2001 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements

More information

CISC 360. Machine-Level Programming II: Control Flow Sep 17, class06

CISC 360. Machine-Level Programming II: Control Flow Sep 17, class06 CISC 360 Machine-Level Programming II: Control Flow Sep 17, 2009 class06 Condition Codes 2 Setting Condition Codes (cont.) 3 Setting Condition Codes (cont.) 4 Reading Condition Codes SetX Condition Description

More information

Page 1. Condition Codes CISC 360. Machine-Level Programming II: Control Flow Sep 17, Setting Condition Codes (cont.)

Page 1. Condition Codes CISC 360. Machine-Level Programming II: Control Flow Sep 17, Setting Condition Codes (cont.) CISC 360 Condition Codes Machine-Level Programming II: Control Flow Sep 17, 2009 class06 2 Setting Condition Codes (cont.) Setting Condition Codes (cont.) 3 4 Page 1 Reading Condition Codes Reading Condition

More information

Control flow. Condition codes Conditional and unconditional jumps Loops Switch statements

Control flow. Condition codes Conditional and unconditional jumps Loops Switch statements Control flow Condition codes Conditional and unconditional jumps Loops Switch statements 1 Conditionals and Control Flow Familiar C constructs l l l l l l if else while do while for break continue Two

More information

Assembly II: Control Flow

Assembly II: Control Flow Assembly II: Control Flow Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu SSE2030: Introduction to Computer Systems, Spring 2018, Jinkyu Jeong (jinkyu@skku.edu)

More information

Assembly III: Procedures. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung Assembly III: Procedures Jo, Heeseung IA-32 Stack (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

ASSEMBLY III: PROCEDURES. Jo, Heeseung

ASSEMBLY III: PROCEDURES. Jo, Heeseung ASSEMBLY III: PROCEDURES Jo, Heeseung IA-32 STACK (1) Characteristics Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address - address of top

More information

Instruction Set Architecture

Instruction Set Architecture CS:APP Chapter 4 Computer Architecture Instruction Set Architecture Randal E. Bryant adapted by Jason Fritts http://csapp.cs.cmu.edu CS:APP2e Hardware Architecture - using Y86 ISA For learning aspects

More information

Condition Codes. Lecture 4B Machine-Level Programming II: Control Flow. Setting Condition Codes (cont.) Setting Condition Codes (cont.

Condition Codes. Lecture 4B Machine-Level Programming II: Control Flow. Setting Condition Codes (cont.) Setting Condition Codes (cont. Lecture 4B Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements Condition Codes Single Bit Registers CF Carry

More information

Lab 6: Conditional Processing

Lab 6: Conditional Processing COE 205 Lab Manual Lab 6: Conditional Processing Page 56 Lab 6: Conditional Processing Contents 6.1. Unconditional Jump 6.2. The Compare Instruction 6.3. Conditional Jump Instructions 6.4. Finding the

More information

Handout #2: Machine-Level Programs

Handout #2: Machine-Level Programs 15-213 Handout #2: Machine-Level Programs on Linux/IA32 Randal E. Bryant David R. O Hallaron October 15, 1999 1 Introduction This document describes how machine-level programs are encoded for Intel IA32

More information

Jump instructions. Unconditional jumps Direct jump. do not change flags. jmp label

Jump instructions. Unconditional jumps Direct jump. do not change flags. jmp label do not change flags Unconditional jumps Direct jump jmp label Jump instructions jmp Continue xor eax,eax Continue: xor ecx,ecx Machine code: 0040340A EB 02 0040340C 33 C0 0040340E 33 C9 displacement =

More information

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110 Questions 1 Question 4.1 1: (Solution, p 5) Define the fetch-execute cycle as it relates to a computer processing a program. Your definition should describe the primary purpose of each phase. Question

More information

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29 Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure

More information

Second Part of the Course

Second Part of the Course CSC 2400: Computer Systems Towards the Hardware 1 Second Part of the Course Toward the hardware High-level language (C) assembly language machine language (IA-32) 2 High-Level Language g Make programming

More information

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Assembly III: Procedures Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu IA-32 (1) Characteristics Region of memory managed with stack discipline

More information

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

CISC 360. Machine-Level Programming II: Control Flow Sep 23, 2008

CISC 360. Machine-Level Programming II: Control Flow Sep 23, 2008 CISC 360 Machine-Level Programming II: Control Flow Sep 23, 2008 class06 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements Condition Codes Single Bit

More information

Machine-level Programming (3)

Machine-level Programming (3) Machine-level Programming (3) Procedures A: call A call A return Two issues How to return to the correct position? How to pass arguments and return values between callee to caller? 2 Procedure Control

More information

Page # CISC 360. Machine-Level Programming II: Control Flow Sep 23, Condition Codes. Setting Condition Codes (cont.)

Page # CISC 360. Machine-Level Programming II: Control Flow Sep 23, Condition Codes. Setting Condition Codes (cont.) CISC 360 Machine-Level Programming II: Control Flow Sep 23, 2008 class06 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements Condition Codes Single Bit

More information

Machine-Level Programming III: Procedures

Machine-Level Programming III: Procedures Machine-Level Programming III: Procedures IA32 Region of memory managed with stack discipline Grows toward lower addresses Register indicates lowest stack address address of top element Bottom Increasing

More information

CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag. ! CF set if carry out from most significant bit. "Used to detect unsigned overflow

CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag. ! CF set if carry out from most significant bit. Used to detect unsigned overflow Lecture 4B Machine-Level Programming II: Control Flow Topics! Condition Codes " Setting " Testing! Control Flow " If-then-else " Varieties of Loops " Switch Statements Condition Codes Single Bit Registers

More information

Conditional Processing

Conditional Processing ١ Conditional Processing Computer Organization & Assembly Language Programming Dr Adnan Gutub aagutub at uqu.edu.sa Presentation Outline [Adapted from slides of Dr. Kip Irvine: Assembly Language for Intel-Based

More information

CSE 351 Midterm Exam

CSE 351 Midterm Exam University of Washington Computer Science & Engineering Winter 2018 Instructor: Mark Wyse February 5, 2018 CSE 351 Midterm Exam Last Name: First Name: SOLUTIONS UW Student ID Number: UW NetID (username):

More information

from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Instruction Set by Randall Hyde

from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Instruction Set by Randall Hyde from WRITE GREAT CODE Volume 2: Thinking Low-Level, Writing High-Level ONLINE APPENDIX A The Minimal 80x86 Set by Randall Hyde San Francisco WRITE GREAT CODE, Volume 2. Copyright 2006 by Randall Hyde.

More information

Giving credit where credit is due

Giving credit where credit is due CSCE 230J Computer Organization Machine-Level Programming II: Control Flow Dr. Steve Goddard goddard@cse.unl.edu Giving credit where credit is due Most of slides for this lecture are based on slides created

More information

IFE: Course in Low Level Programing. Lecture 6

IFE: Course in Low Level Programing. Lecture 6 IFE: Course in Low Level Programing Lecture 6 Instruction Set of Intel x86 Microprocessors Conditional jumps Jcc jump on condition cc, JMP jump always, CALL call a procedure, RET return from procedure,

More information

CS241 Computer Organization Spring 2015 IA

CS241 Computer Organization Spring 2015 IA CS241 Computer Organization Spring 2015 IA-32 2-10 2015 Outline! Review HW#3 and Quiz#1! More on Assembly (IA32) move instruction (mov) memory address computation arithmetic & logic instructions (add,

More information

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

Computer Architecture..Second Year (Sem.2).Lecture(4) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات مدرس المادة : م. سندس العزاوي... قسم / الحاسبات... - 26 27 Assembly Level Machine Organization Usage of AND, OR, XOR, NOT AND : X Y X AND Y USE : to chick any bit by change ( to ) or ( to ) EX : AX = FF5

More information

CS 290 Host-based Security and Malware. Christopher Kruegel

CS 290 Host-based Security and Malware. Christopher Kruegel CS 290 Host-based Security and Malware Christopher Kruegel chris@cs.ucsb.edu Reverse Engineering Introduction Reverse engineering process of analyzing a system understand its structure and functionality

More information

CS213. Machine-Level Programming III: Procedures

CS213. Machine-Level Programming III: Procedures CS213 Machine-Level Programming III: Procedures Topics IA32 stack discipline Register saving conventions Creating pointers to local variables IA32 Region of memory managed with stack discipline Grows toward

More information

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday

Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday Questions about last homework? (Would more feedback be useful?) New reading assignment up: due next Monday addl: bitwise for signed (& unsigned) 4 bits: 1000 = -8, 0111 = 7-8 + -8 = -16 = 0 1000 + 1000

More information

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. CS 33 Machine Programming (2) CS33 Intro to Computer Systems XI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved. Observations about arith int arith(int x, int y, int z) { int t1 = x+y; int t2

More information

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS 33. Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. CS 33 Machine Programming (2) CS33 Intro to Computer Systems XII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved. Processor State (x86-64, Partial) %rax %eax %r8 %r8d %rbx %ebx %r9 %r9d %rcx %ecx

More information

CPSC W Term 2 Problem Set #3 - Solution

CPSC W Term 2 Problem Set #3 - Solution 1. (a) int gcd(int a, int b) { if (a == b) urn a; else if (a > b) urn gcd(a - b, b); else urn gcd(a, b - a); CPSC 313 06W Term 2 Problem Set #3 - Solution.file "gcdrec.c".globl gcd.type gcd, @function

More information

Lab 3. The Art of Assembly Language (II)

Lab 3. The Art of Assembly Language (II) Lab. The Art of Assembly Language (II) Dan Bruce, David Clark and Héctor D. Menéndez Department of Computer Science University College London October 2, 2017 License Creative Commons Share Alike Modified

More information

5.1. CS356 Unit 5. x86 Control Flow

5.1. CS356 Unit 5. x86 Control Flow 5.1 CS356 Unit 5 x86 Control Flow 5.2 Compiler output ASSEMBLY TRANSLATION EXAMPLE 5.3 Translation to Assembly We will now see some C code and its assembly translation A few things to remember: Data variables

More information

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27 Procedure Calls Young W. Lim 2016-11-05 Sat Young W. Lim Procedure Calls 2016-11-05 Sat 1 / 27 Outline 1 Introduction References Stack Background Transferring Control Register Usage Conventions Procedure

More information

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p text C program (p1.c p2.c) Compiler (gcc -S) text Asm

More information

CSCI 2021: x86-64 Control Flow

CSCI 2021: x86-64 Control Flow CSCI 2021: x86-64 Control Flow Chris Kauffman Last Updated: Mon Mar 11 11:54:06 CDT 2019 1 Logistics Reading Bryant/O Hallaron Ch 3.6: Control Flow Ch 3.7: Procedure calls Goals Jumps and Control flow

More information

Assembly Language: Part 2

Assembly Language: Part 2 Assembly Language: Part 2 1 Goals of this Lecture Help you learn: Intermediate aspects of IA-32 assembly language Control flow with signed integers Control flow with unsigned integers Arrays Structures

More information

Branching and Looping

Branching and Looping Branching and Looping EECE416 uc Fall 2011 Unconditional Jumps jmp Like a goto in a high-level language Format: jmp StatementLabel The next statement executed will be the one at StatementLabel: jmp Encoding

More information

An Introduction to x86 ASM

An Introduction to x86 ASM An Introduction to x86 ASM Malware Analysis Seminar Meeting 1 Cody Cutler, Anton Burtsev Registers General purpose EAX, EBX, ECX, EDX ESI, EDI (index registers, but used as general in 32-bit protected

More information

Intro to GNU Assembly Language on Intel Processors

Intro to GNU Assembly Language on Intel Processors Intro to GNU Assembly Language on Intel Processors Prof. Godfrey C. Muganda North Central College February 29, 2004 1 Basic Machine Architecture This family of processors has a 32-bit architecture: its

More information

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View Computer Architecture I Instruction Set Architecture Assembly Language View Processor state Registers, memory, Instructions addl, movl, andl, How instructions are encoded as bytes Layer of Abstraction

More information

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012 19:211 Computer Architecture Lecture 10 Fall 20 Topics:Chapter 3 Assembly Language 3.2 Register Transfer 3. ALU 3.5 Assembly level Programming We are now familiar with high level programming languages

More information

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans. INSTRUCTOR: ABDULMUTTALIB A H ALDOURI Conditional Jump Cond Unsigned Signed = JE : Jump Equal JE : Jump Equal ZF = 1 JZ : Jump Zero JZ : Jump Zero ZF = 1 JNZ : Jump Not Zero JNZ : Jump Not Zero ZF = 0

More information

Computer Systems C S Cynthia Lee

Computer Systems C S Cynthia Lee Computer Systems C S 1 0 7 Cynthia Lee 2 Today s Topics LECTURE: More assembly code! NEXT TIME: More control flow Some misc. instructions you might see in your assign5 binary bomb Details of function call

More information

CSE351 Spring 2018, Midterm Exam April 27, 2018

CSE351 Spring 2018, Midterm Exam April 27, 2018 CSE351 Spring 2018, Midterm Exam April 27, 2018 Please do not turn the page until 11:30. Last Name: First Name: Student ID Number: Name of person to your left: Name of person to your right: Signature indicating:

More information

x86-64 Programming II

x86-64 Programming II x86-64 Programming II CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants: Kevin Bi Parker DeWilde Emily Furst Sarah House Waylon Huang Vinny Palaniappan http://xkcd.com/409/ Administrative Homework

More information

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 15-213 The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables class07.ppt

More information

Machine- level Programming II: Control Flow

Machine- level Programming II: Control Flow Machine- level Programming II: Control Flow Topics Condi;on Codes Se=ng Tes;ng Control Flow If- then- else Varie;es of Loops Switch Statements 1! Condi;on Codes Single Bit Registers CF Carry Flag SF Sign

More information

x86 Programming II CSE 351 Winter

x86 Programming II CSE 351 Winter x86 Programming II CSE 351 Winter 2017 http://xkcd.com/1652/ Administrivia 2 Address Computation Instruction v leaq src, dst lea stands for load effective address src is address expression (any of the

More information