Appendix E: C Idioms in Assembly Language


This appendix is for C programmers learning assembly language. It is meant to give a general idea about how C constructs can be implemented in assembly language.

If Statement

In C, an if statement consists of three parts - the condition, the true branch, and the false branch. However, since assembly language is not a block structured language, you have to work a little to implement the block-like nature of C. For example, look at the following C code:

 if (a == b) {  /* True Branch Code Here */ } else {  /* False Branch Code Here */ } /* At This Point, Reconverge */ 

In assembly language, this can be rendered as:

  #Move a and b into registers for comparison  movl a, %eax  movl b, %ebx  #Compare  cmpl %eax, %ebx  #If True, go to true branch  je true_branch  false_branch:  #This label is unnecessary,                #only here for documentation  #False Branch Code Here  #Jump to recovergence point  jmp reconverge true_branch:  #True Branch Code Here reconverge:  #Both branches recoverge to this point 

As you can see, since assembly language is linear, the blocks have to jump around each other. Recovergence is handled by the programmer, not the system.

A case statement is written just like a sequence of if statements.




Programming from the Ground Up
Programming from the Ground Up
ISBN: 0975283847
EAN: 2147483647
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net