13 Assembly

13 Assembly - 3/17/11 1 CMSC 216 Introduction to Computer...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 3/17/11 1 CMSC 216 Introduction to Computer Systems Lecture 13 Assembly Language, cont. Jan Plane & Pete Keleher {jplane, keleher}@cs.umd.edu Administrivia Project 3b questions? Continue reading Bryant and OHallaron Section 4.1 (Y86 subset) and Chapter 3, for more info on IA-32 instruction set architecture CMSC 216 - Wood, Sussman, Herman, Plane 2 3/17/11 2 A SSEMBLY L ANGUAGE ( CONT .) Chapters 3 and 4.1, Bryant and O'Hallaron CMSC 216 - Wood, Sussman, Herman, Plane 3 Translating C to Y86 This process is not always straightforward Even simple statements like " a = b * c; " can require several instructions: irmovl B,%eax mrmovl 0(%eax),%ebx irmovl C,%eax mrmovl 0(%eax),%ecx multl %ebx,%ecx irmovl A,%eax rmmovl %ecx,0(%eax) CMSC 216 - Wood, Sussman, Herman, Plane 4 3/17/11 3 Register spilling We only have a limited number of registers What happens if we need to keep more than 8 values around at once? If we run out of registers to store our data, we need to use memory to store values We can use the stack or define static arrays (as we'll see later) CMSC 216 - Wood, Sussman, Herman, Plane 5 Translating branches Consider the following C code: if (i == j) printf("="); else printf("X"); printf("\n"); CMSC 216 - Wood, Sussman, Herman, Plane 6 if (i == j) goto Equal; printf("X"); goto EndIf; Equal: printf("="); EndIf: printf("\n"); 3/17/11 4 Translating branches, cont. We can then take the labeled C code and translate it in a fairly straightforward fashion: subl %eax,%ebx # i:%eax,j:%ebx je Equal irmovl $88,%ecx # else block wrch %ecx jmp EndIf Equal: irmovl $61,%ecx # true block wrch %ecx EndIf: irmovl $10,%ecx # after if/else wrch %ecx CMSC 216 - Wood, Sussman, Herman, Plane 7 Translating loops What about C code like this? do { ... } while (condition); This can be translated simply: Loop: ... # begin loop body # evaluate condition je Loop # jump back if true Note that you can use whatever jump instruction/condition is appropriate CMSC 216 - Wood, Sussman, Herman, Plane 8 3/17/11 5 Translating while loops while loops are a bit different; but these can be written as do-while loops, with a little modification while (condition) do_something(); if (condition) { do { do_something(); } while(condition); } CMSC 216 - Wood, Sussman, Herman, Plane 9 if (! condition) goto EndWhile; do { do_something(); } while (condition); EndWhile: Translating while loops, cont. Assuming %eax always holds the value of our condition (and we keep 0 stored in %edi ): irmovl $0,%edi addl %edi,%eax je EndWhile Loop: # code for do_something(); addl %edi,%eax jne Loop EndWhile: CMSC 216 - Wood, Sussman, Herman, Plane 10 3/17/11 6 Translating for loops For loops are very similar to while loops; we can use this fact to convert a for loop into a form we know how to work with How do we convert this into a while loop?...
View Full Document

This note was uploaded on 11/16/2011 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 63

13 Assembly - 3/17/11 1 CMSC 216 Introduction to Computer...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online