<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Andrew Harvey&#039;s Blog &#187; comp2121</title>
	<atom:link href="http://andrewharvey4.wordpress.com/tag/comp2121/feed/" rel="self" type="application/rss+xml" />
	<link>http://andrewharvey4.wordpress.com</link>
	<description></description>
	<lastBuildDate>Fri, 18 Dec 2009 23:25:25 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='andrewharvey4.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/574c934288b8ec9b24c519954a9bc2be?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Andrew Harvey&#039;s Blog &#187; comp2121</title>
		<link>http://andrewharvey4.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://andrewharvey4.wordpress.com/osd.xml" title="Andrew Harvey&#039;s Blog" />
		<item>
		<title>COMP2121 Quick Summary on Particular Aspects</title>
		<link>http://andrewharvey4.wordpress.com/2009/06/23/comp2121-quick-summary-on-particular-aspects/</link>
		<comments>http://andrewharvey4.wordpress.com/2009/06/23/comp2121-quick-summary-on-particular-aspects/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 10:50:43 +0000</pubDate>
		<dc:creator>Andrew Harvey</dc:creator>
				<category><![CDATA[comp2121]]></category>
		<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://andrewharvey4.wordpress.com/?p=571</guid>
		<description><![CDATA[Von-Newman vs. Harvard Architecture.
Von-Newman has a single memory space, share for data and program instructions. Harvard Architecture has separate memory spaces for data and instructions (so you cannot execute from the data memory).
2&#8217;s compliment

It is important to know that the hardware does all arithmetic in 2&#8217;s compliment. It is up to the programmer to interpret [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=571&subd=andrewharvey4&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2>Von-Newman vs. Harvard Architecture.</h2>
<p>Von-Newman has a single memory space, share for data and program instructions. Harvard Architecture has separate memory spaces for data and instructions (so you cannot execute from the data memory).</p>
<h2>2&#8217;s compliment</h2>
<p><a href="http://andrewharvey4.files.wordpress.com/2009/06/twos_compliment.png"><img class="size-full wp-image-594 aligncenter" title="twos_compliment" src="http://andrewharvey4.files.wordpress.com/2009/06/twos_compliment.png?w=180&#038;h=278" alt="twos_compliment" width="180" height="278" /></a></p>
<p>It is important to know that the hardware does all arithmetic in 2&#8217;s compliment. It is up to the programmer to interpret the number as signed or unsigned.</p>
<p>To convert a number from 2&#8217;s compliment, for example -45 in 2&#8217;s compliment is 11010011, we can do something like this,</p>
<p><img src='http://l.wordpress.com/latex.php?latex=1+%5Ctimes+%28-2%5E7%29+%2B+1+%5Ctimes+2%5E6+%2B+1+%5Ctimes+2%5E6+%2B+0+%5Ctimes+2%5E5+%2B1+%5Ctimes+2%5E4+%2B+0+%5Ctimes+2%5E3+%2B+0+%5Ctimes+2%5E2+%2B+1+%5Ctimes+2%5E1+%2B+1+%5Ctimes+2%5E0+%3D+-45&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='1 \times (-2^7) + 1 \times 2^6 + 1 \times 2^6 + 0 \times 2^5 +1 \times 2^4 + 0 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = -45' title='1 \times (-2^7) + 1 \times 2^6 + 1 \times 2^6 + 0 \times 2^5 +1 \times 2^4 + 0 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = -45' class='latex' /></p>
<p>To go the other way from say -1 to the 2&#8217;s compliment form 11111111 we use that <img src='http://l.wordpress.com/latex.php?latex=2%5Ep+-+X&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^p - X' title='2^p - X' class='latex' /> formula. I&#8217;m not exactly sure how its supposed to work so I&#8217;ve hacked it to make it work.</p>
<p>If the number you wish to convert is negative, let <img src='http://l.wordpress.com/latex.php?latex=X+%3D+-n&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='X = -n' title='X = -n' class='latex' />, so that X is positive then take <img src='http://l.wordpress.com/latex.php?latex=2%5Ep&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^p' title='2^p' class='latex' /> where <em>p</em> is the number of bits you are using (say 8), then subtract X. If the number to convert is less than <img src='http://l.wordpress.com/latex.php?latex=2%5Ep&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^p' title='2^p' class='latex' /> (where <em>p</em> is the number of bits, say 8 ) then leave it as is and that in your 2&#8217;s compliment.</p>
<p>Now that was complicated. But its the only way I can get that advertised <img src='http://l.wordpress.com/latex.php?latex=2%5Ep+-+X&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^p - X' title='2^p - X' class='latex' /> formula to work with the given set of sample data (as in that table above).</p>
<h2>Sign Extension</h2>
<p>Why do we need sign extension? We need it in order to do operations on numbers than have different bit lengths (the number of bits used to represent the number).</p>
<h2>Decimal to Binary</h2>
<p>From a human kind of approach to convert 221 to binary, we see that <img src='http://l.wordpress.com/latex.php?latex=2%5E7+%3D+128&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^7 = 128' title='2^7 = 128' class='latex' />, that is 7 is the largest power of 2 less than 221, so we have <img src='http://l.wordpress.com/latex.php?latex=1+%5Ctimes+2%5E7&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='1 \times 2^7' title='1 \times 2^7' class='latex' />. That gives us 128, so we still have 93 (221-128) to go. We try <img src='http://l.wordpress.com/latex.php?latex=2%5E6&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^6' title='2^6' class='latex' />, this is less than 93. So far we have <img src='http://l.wordpress.com/latex.php?latex=1+%5Ctimes+2%5E7+%2B+1+%5Ctimes+2%5E6&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='1 \times 2^7 + 1 \times 2^6' title='1 \times 2^7 + 1 \times 2^6' class='latex' />. 29 left now, but <img src='http://l.wordpress.com/latex.php?latex=2%5E5&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^5' title='2^5' class='latex' /> is greater than 29, so we put a zero in that digit, ie. <img src='http://l.wordpress.com/latex.php?latex=1+%5Ctimes+2%5E7+%2B+1+%5Ctimes+2%5E6+%2B+0+%5Ctimes+2%5E5&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='1 \times 2^7 + 1 \times 2^6 + 0 \times 2^5' title='1 \times 2^7 + 1 \times 2^6 + 0 \times 2^5' class='latex' />. If we go on we get <img src='http://l.wordpress.com/latex.php?latex=1+%5Ctimes+2%5E7+%2B+1+%5Ctimes+2%5E6+%2B+0+%5Ctimes+2%5E5+%2B+1+%5Ctimes+2%5E4+%2B+1+%5Ctimes+2%5E3+%2B+1+%5Ctimes+2%5E2+%2B+0+%5Ctimes+2%5E1+%2B+1+%5Ctimes+2%5E0&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='1 \times 2^7 + 1 \times 2^6 + 0 \times 2^5 + 1 \times 2^4 + 1 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0' title='1 \times 2^7 + 1 \times 2^6 + 0 \times 2^5 + 1 \times 2^4 + 1 \times 2^3 + 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0' class='latex' />. Taking the coefficients of the <img src='http://l.wordpress.com/latex.php?latex=%5Ctimes+2%5Ex&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='\times 2^x' title='\times 2^x' class='latex' /> terms we get the number 221 in binary, 11011101.</p>
<p>We can convert hexadecimal to binary by going from hex to decimal then decimal to binary. For hex to decimal,</p>
<p><img src='http://l.wordpress.com/latex.php?latex=%5Cmbox%7BF23AC%7D+%3D+15+%5Ctimes+16%5E4+%2B+2+%5Ctimes+16%5E3+%2B+3+%5Ctimes+16%5E2+%2B+10+%5Ctimes+16%5E1+%2B+12+%5Ctimes+16%5E0+%3D+992172&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='\mbox{F23AC} = 15 \times 16^4 + 2 \times 16^3 + 3 \times 16^2 + 10 \times 16^1 + 12 \times 16^0 = 992172' title='\mbox{F23AC} = 15 \times 16^4 + 2 \times 16^3 + 3 \times 16^2 + 10 \times 16^1 + 12 \times 16^0 = 992172' class='latex' /><br />
(where F23AC is in hex and 992172 is in decimal)</p>
<h2>Operations on Signed and Unsigned Multi byte Numbers</h2>
<pre>add al, bl
adc ah, bh
does a + b, result in is a.</pre>
<p>There are 3 multiplication operations, MUL (Multiply Unsigned), MULS (Multiply Signed) and MULSU (Multiply Signed with Unsigned). They each do this. Notice the result is stored in r1:r0.</p>
<pre><a href="http://andrewharvey4.files.wordpress.com/2009/06/mul.png"><img class="aligncenter size-full wp-image-603" title="mul" src="http://andrewharvey4.files.wordpress.com/2009/06/mul.png?w=450&#038;h=60" alt="mul" width="450" height="60" /></a>Thus to do n*m = r where n is 2 bytes unsigned and m is 1 byte signed,
mulsu nl, m ;nl * (signed)m
movw rh:rl, r1:r0
mulsu nh, m ;(signed)nh * m
add rh, r0</pre>
<p>We can also do 16bit * 16bit,</p>
<pre>;* From AVR Instruction Set Guide, pg 99-100.
;* Signed multiply of two 16-bit numbers with 32-bit result.
;* r19:r18:r17:r16 = r23:r22 * r21:r20
muls16x16_32:
clr r2
muls r23, r21 ;(signed)ah * (signed)bh
movw r19:r18, r1:r0
mul r22, r20 ;al * bl
movw r17:r16, r1:r0
mulsu r23, r20 ;(signed)ah * bl
sbc r19, r2
add r17, r0
adc r18, r1
adc r19, r2
mulsu r21, r22 ;(signed)bh * al
sbc r19, r2
add r17, r0
adc r18, r1
adc r19, r2
ret</pre>
<h2>brge and brsh</h2>
<ul>
<li>brge is Branch if Greater or Equal, Signed.<br />
if (<img src='http://l.wordpress.com/latex.php?latex=N+%5Coplus+V+%3D+0&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='N \oplus V = 0' title='N \oplus V = 0' class='latex' />) then branch.<br />
When you do cp Rd, Rr then brge, the branch will be taken if Rd <img src='http://l.wordpress.com/latex.php?latex=%5Cge&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='\ge' title='\ge' class='latex' /> Rr, where Rd and Rr are taken to be signed numbers.</li>
<li>brsh is Branch if Same or Higher.<br />
if (<img src='http://l.wordpress.com/latex.php?latex=C+%3D+0&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='C = 0' title='C = 0' class='latex' />) then branch.<br />
When you do cp Rd, Rr then brsh, the branch will be taken if Rd <img src='http://l.wordpress.com/latex.php?latex=%5Cge&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='\ge' title='\ge' class='latex' /> Rr, where Rd and Rr are taken to be unsigned numbers.</li>
</ul>
<h2>Calculating Total Stack Space Needed</h2>
<p>Draw a call tree, find the path with the most total weight, that total weight is the total stack size needed. Here is the sample question,</p>
<blockquote><p>A C program consists of five functions. Their calling relations are shown as follows (the arguments and irrelevant C statements are omitted).</p>
<pre><code>int main(void) {</code>
<code>  …</code>
<code>  func1(…);</code>
<code>  func2(…);</code>
<code>  …</code>
<code>}</code></pre>
<pre>int func1(…) {
  …
  func1(…);
  …
}

int func2(…) {
  …
  func3(…);
  func4(…);
  …
}</pre>
<p>func1() is a recursive function and calls itself 15 times for the actual parameters given in main(). Both func3() and func4() do not call any function. The sizes of all stack frames are shown as follows.</p>
<p style="padding-left:30px;">main(): 200 bytes.<br />
func1(): 100 bytes.<br />
func2(): 400 bytes.<br />
func3(): 1,400 bytes.<br />
func4(): 300 bytes.</p>
<p>How much stack space is needed to execute this program correctly? (3 marks)</p></blockquote>
<p><a href="http://andrewharvey4.files.wordpress.com/2009/06/call_graph.png"><img class="aligncenter size-full wp-image-574" title="call_graph" src="http://andrewharvey4.files.wordpress.com/2009/06/call_graph.png?w=450&#038;h=241" alt="call_graph" width="450" height="241" /></a>There are three paths,</p>
<table style="height:51px;" border="1" cellspacing="0" cellpadding="4" width="339">
<col width="85"></col>
<col width="85"></col>
<col width="85"></col>
<tbody>
<tr valign="top">
<td width="33%">main()<br />
func1()<br />
func1() x 15<br />
200+100+15&#215;100<br />
=1800</td>
<td width="33%">main()<br />
func2()<br />
func3()<br />
200+400+1400<br />
=2000</td>
<td width="33%">main()<br />
func2()<br />
func4()<br />
200+400+300<br />
=900</td>
</tr>
</tbody>
</table>
<p>The path with the most total weight is main() &gt; func2() &gt; func3(), so this is the total stack space needed.</p>
<h2>Nested Interrupts</h2>
<p style="text-align:center;"><a href="http://andrewharvey4.files.wordpress.com/2009/06/nested_interrupt.png"><img class="aligncenter size-full wp-image-584" title="nested_interrupt" src="http://andrewharvey4.files.wordpress.com/2009/06/nested_interrupt.png?w=448&#038;h=334" alt="nested_interrupt" width="448" height="334" /></a>(Source: Hui Wu&#8217;s Lecture Notes)</p>
<h2>Keypads with &#8216;abc&#8217; &#8216;def&#8217; &#8230; buttons</h2>
<p>These keypads where to enter b you need to press the abc button twice in succession, but wait to long at it will chose a. Here is a psudo algorithm that seemed to fit this,</p>
<pre style="padding-left:30px;">.def reg = rN
.def reg = rM
.def count = rX

//passvalue means that we register the given value ie. abc abc wait &gt; b

setup:
clr reg (to some value that is != to a key value) ;set to default
clr count
rjmp keyloop

keyloop:
  check pins for a key
  if no key pressed rjmp keyloop, else continue

  //key was pressed, and value is stored in key
  reset someTimeCounter
  if (key == reg) {
     inc count
     if (count == 3)
        passvalue(reg,count)
  }else{
     if (reg != default) ;so we don't initially passvalue
        passvalue(reg,count) ;send the last value
     reg = key ;store the new one
     count = 1
  }

rjmp keyloop

if someTimeCounter expires and count != 0 //(count up, so expires after time to wait for anymore keypresses) (check count != 0, because if its 0 then we never had any key pressed that we need to send)
   passvalue(reg,count)
   reg = default</pre>
<h2>Switch Bounce Software Solution</h2>
<p>When a switch makes contact, its mechanical springiness will cause the contact to bounce, or make and break, for a few millisecond (typically 5 to 10 ms). Two software solutions are wait and see and counter-based.</p>
<ol>
<li>If we detect it as closed, wait for a little bit and check again.</li>
<li>Poll the switch constantly. For each poll if the switch is closed increment the counter. If we reach a certain value in a certain time then the switch was closed (or button pressed).</li>
</ol>
<h2>Serial Communication (Start and Stop bit)</h2>
<p>&#8220;[The] start bit is used to indicate the start of a frame. Without the start bit, the receiver cannot distinguish between the idle line and the 1 bit because both are logical one. A stop bit is used to allow the receiver to transfer the data from the receive buffer to the memory.&#8221; (Wu, Homework 6 Solutions)</p>
<h2>UART</h2>
<p style="text-align:center;"><a href="http://andrewharvey4.files.wordpress.com/2009/06/uart.png"><img class="aligncenter size-full wp-image-595" title="uart" src="http://andrewharvey4.files.wordpress.com/2009/06/uart.png?w=450&#038;h=176" alt="uart" width="450" height="176" /></a>(Source: Hui Wu, Lecture Notes)</p>
<h2>Sample Q3a</h2>
<p>(This code probably won&#8217;t work and probably has errors (and maybe not just simple ones, but serious ones that mean that the logic is wrong))</p>
<pre>.dseg
A: .byte 20 ;array of size 10, element size 2 bytes

.cseg
ldi XL, low(A)
ldi XH, high(A)

;add the contents of the array.
store 0
store 1
store 2
store 3
store 4
store 5
store 6
store 7
store 8
store 9

;find the largest value
ldi XL, low(A)
ldi XH, high(A)

;start with the 1st element of the array
ld r25, X+
ld r26, X+

ldi r20, 10 ;size of array
loop:
 cpi r20, 0
 breq endloop

 ld r21, X+
 ld r22, X+

 cp r25, r21
 cpc r26, c22
 brlo lowerthan
 ;we have a new max
 mov r25, r21
 mov r26, r22

 lowerthan:

 inc r20
 rjmp loop
endloop: rjmp endloop

.macro store
ldi r16, low(@0)
ldi r17, high(@0)

st X+, r16
st X+, r17
.endmacro</pre>
<p>For some reason in my lecture notes I have &#8220;eg. fine 2nd or 3rd smallest or largest&#8221; so here is a modification to do something like that.</p>
<pre>.dseg
A: .byte 20 ;array of size 10, element size 2 bytes

.cseg
ldi XL, low(A)
ldi XH, high(A)

;add the contents of the array.
store 0
store 1
store 2
store 3
store 4
store 5
store 6
store 7
store 8
store 9

;sort into accending
loopthough for the length of array, (by then we can be sure its sorted)
ldi r23, 10
largeloop:
 cpi r23, 0
 breq endlargeloop

 ;point X to the start of A
 ldi XL, low(A)
 ldi XH, high(A)

 ;start with the 1st element of the array
 ld r25, X+
 ld r26, X+

 ldi r20, 10 ;size of array
 loop:
 cpi r20, 0
 breq endloop

 ;the next value
 ld r21, X+
 ld r22, X+

 cp r25, r21
 cpc r26, c22
 brge gethan
 ;r22:r21 &lt; r26:r25
 ;swap the order
 st -X, r26
 st -X, r25
 st -X, r22
 st -X, r21

 ld r24, X+ ;to change the X pointer
 ld r24, X+

 ld r25, X+
 ld r26, X+

 gethan:

 inc r20
 rjmp loop
 endloop:
endlargeloop:

inf: rjmp inf

.macro store
ldi r16, low(@0)
ldi r17, high(@0)

st X+, r16
st X+, r17
.endmacro</pre>
Posted in comp2121 Tagged: comp2121, computing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrewharvey4.wordpress.com/571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrewharvey4.wordpress.com/571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrewharvey4.wordpress.com/571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrewharvey4.wordpress.com/571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrewharvey4.wordpress.com/571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrewharvey4.wordpress.com/571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrewharvey4.wordpress.com/571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrewharvey4.wordpress.com/571/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrewharvey4.wordpress.com/571/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrewharvey4.wordpress.com/571/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=571&subd=andrewharvey4&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://andrewharvey4.wordpress.com/2009/06/23/comp2121-quick-summary-on-particular-aspects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/28043c88c4d0c2700ec793045d0ebe91?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">andrewharvey4</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/06/twos_compliment.png" medium="image">
			<media:title type="html">twos_compliment</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/06/mul.png" medium="image">
			<media:title type="html">mul</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/06/call_graph.png" medium="image">
			<media:title type="html">call_graph</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/06/nested_interrupt.png" medium="image">
			<media:title type="html">nested_interrupt</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/06/uart.png" medium="image">
			<media:title type="html">uart</media:title>
		</media:content>
	</item>
		<item>
		<title>COMP2121 &#8211; Wk05 &#8211; Interrupts</title>
		<link>http://andrewharvey4.wordpress.com/2009/06/20/comp2121-wk05-interrupts/</link>
		<comments>http://andrewharvey4.wordpress.com/2009/06/20/comp2121-wk05-interrupts/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 04:33:50 +0000</pubDate>
		<dc:creator>Andrew Harvey</dc:creator>
				<category><![CDATA[comp2121]]></category>
		<category><![CDATA[unswcourse]]></category>
		<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://andrewharvey4.wordpress.com/?p=455</guid>
		<description><![CDATA[To explain interrupts, Wu used an example of a network card that is downloading a file. The network card has a buffer, and only once this buffer is full (or data stream is complete) should the CPU then copy the contents from the buffer to the RAM. So how does the CPU know when the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=455&subd=andrewharvey4&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>To explain interrupts, Wu used an example of a network card that is downloading a file. The network card has a buffer, and only once this buffer is full (or data stream is complete) should the CPU then copy the contents from the buffer to the RAM. So how does the CPU know when the network card&#8217;s buffer is full and when to execute the copy? He described two ways here <strong>interrupt</strong> and <strong>polling</strong>.</p>
<p>Polling involves the CPU periodically asking the network card, are you full? Two problems with this method are a) there may be a delay as you have to wait for the poll request to be made and b) it wastes a lot of CPU time. Polling is implemented in software, not hardware.</p>
<p>An alternative to polling is using interrupts whereby the network card will send an interrupt signal to the CPU to get its attention. This needs special hardware to implement, however it is very efficient compared with polling.</p>
<p>An interrupt system must (among other things),</p>
<ul>
<li>Wait for the current instruction to finish before taking care of the interrupt.</li>
<li>Return to the interrupted program at the point where it was interrupted.</li>
<li>Signal the interrupting device with an acknowledge signal when the interrupt has been recognised.</li>
</ul>
<p>IRQ is an interrupt request signal.</p>
<p>A daisy chain arrangement (as seen below) allows multiple devices to send and IRQ. However the CPU cannot determine from the IRQ line which device sent the interrupt. So in a daisy chain system when the CPU receives an IRQ, it will send a signal to IO1 asking &#8220;did you send the IRQ?&#8221; if IO1 sent the request it will reply &#8220;yes&#8221;, if not it will pass the question on to the next IO device and so on. The response is passed back in the same way.</p>
<p><a href="http://andrewharvey4.files.wordpress.com/2009/06/daisychain.png"><img class="aligncenter size-full wp-image-576" title="daisyChain" src="http://andrewharvey4.files.wordpress.com/2009/06/daisychain.png?w=297&#038;h=132" alt="daisyChain" width="297" height="132" /></a></p>
<p>Reset is an interrupt in AVR, and in the AVR Mega64 there are five different sources of reset. There is a flag in the MCU Control Register for each of these and can be used to determine the source of a reset interrupt. The watchdog timer is one source of reset.</p>
<h2>Watchdog Timer</h2>
<p>The watchdog timer is used to try to reset the system if an error such hang occurs. The watchdog timer in AVR can be enabled or disabled.</p>
<p>If the Watchdog timer is enabled, it needs to be periodically reset using the wdr instruction. When (if) the Watchdog times out, it will generate a short reset pulse.</p>
Posted in comp2121, unswcourse Tagged: comp2121, computing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrewharvey4.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrewharvey4.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrewharvey4.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrewharvey4.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrewharvey4.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrewharvey4.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrewharvey4.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrewharvey4.wordpress.com/455/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrewharvey4.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrewharvey4.wordpress.com/455/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=455&subd=andrewharvey4&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://andrewharvey4.wordpress.com/2009/06/20/comp2121-wk05-interrupts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/28043c88c4d0c2700ec793045d0ebe91?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">andrewharvey4</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/06/daisychain.png" medium="image">
			<media:title type="html">daisyChain</media:title>
		</media:content>
	</item>
		<item>
		<title>Setting Up the COMP2121 AVR Board at Home on a Linux Machine</title>
		<link>http://andrewharvey4.wordpress.com/2009/05/08/setting-up-the-comp2121-avr-board-at-home-on-a-linux-machine/</link>
		<comments>http://andrewharvey4.wordpress.com/2009/05/08/setting-up-the-comp2121-avr-board-at-home-on-a-linux-machine/#comments</comments>
		<pubDate>Fri, 08 May 2009 02:00:41 +0000</pubDate>
		<dc:creator>Andrew Harvey</dc:creator>
				<category><![CDATA[comp2121]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[avr]]></category>

		<guid isPermaLink="false">http://andrewharvey4.wordpress.com/?p=506</guid>
		<description><![CDATA[Just quickly because I need to get back to work, here is how I managed to finally set up a working environment to use the COMP2121 AVR Board at home using linux.

Installed Sun&#8217;s Virtual Box.
Downloaded and installed Windows 7 Beta (Because its free (as in free beer) and only expires after this course is over)
Under [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=506&subd=andrewharvey4&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Just quickly because I need to get back to work, here is how I managed to finally set up a working environment to use the COMP2121 AVR Board at home using linux.</p>
<ol>
<li>Installed Sun&#8217;s Virtual Box.</li>
<li>Downloaded and installed Windows 7 Beta (Because its free (as in free beer) and only expires after this course is over)</li>
<li>Under the network settings for the Windows 7 virtual machine, disabled it from the rest of the world (I don&#8217;t need it here, so save my download for better things that Windows updates).<br />
<a href="http://andrewharvey4.files.wordpress.com/2009/05/screenshot-2121-settings.png"><img class="aligncenter size-full wp-image-507" title="Virtual Box Network Settings" src="http://andrewharvey4.files.wordpress.com/2009/05/screenshot-2121-settings.png?w=449&#038;h=358" alt="Virtual Box Network Settings" width="449" height="358" /></a></li>
<li>Install AVR Studio.</li>
<li>Install a driver (I used this one <a href="http://www.ftdichip.com/Drivers/CDM/CDM%202.04.16.exe">http://www.ftdichip.com/Drivers/CDM/CDM%202.04.16.exe</a>) from here http://www.ftdichip.com/Drivers/VCP.htm. Don&#8217;t need to worry about trusting anything as this virtual will just be used for the AVR board.</li>
<li>In the bottom right hand of the virtual box window enable the &#8220;FTDI USB &lt;-&gt;UNSW uLab&#8221; USB device (when its plugged in).</li>
<li>Run nite.exe with these arguments &#8221; -l com3 -f 4 -t 1 -h X:\AVRProj\&#8221;. You may need to check if its com3 or something else in &#8220;Device Manager&#8221;. Where that last path is the default directory (I set up a shared directory in the VM settings).</li>
</ol>
Posted in comp2121, computing Tagged: avr, comp2121 <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrewharvey4.wordpress.com/506/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrewharvey4.wordpress.com/506/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrewharvey4.wordpress.com/506/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrewharvey4.wordpress.com/506/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrewharvey4.wordpress.com/506/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrewharvey4.wordpress.com/506/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrewharvey4.wordpress.com/506/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrewharvey4.wordpress.com/506/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrewharvey4.wordpress.com/506/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrewharvey4.wordpress.com/506/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=506&subd=andrewharvey4&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://andrewharvey4.wordpress.com/2009/05/08/setting-up-the-comp2121-avr-board-at-home-on-a-linux-machine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/28043c88c4d0c2700ec793045d0ebe91?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">andrewharvey4</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/05/screenshot-2121-settings.png" medium="image">
			<media:title type="html">Virtual Box Network Settings</media:title>
		</media:content>
	</item>
		<item>
		<title>COMP2121 &#8211; Wk03/04 &#8211; Data Transfer/Functions</title>
		<link>http://andrewharvey4.wordpress.com/2009/04/17/comp2121-wk0304-data-transferfunctions/</link>
		<comments>http://andrewharvey4.wordpress.com/2009/04/17/comp2121-wk0304-data-transferfunctions/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 10:10:48 +0000</pubDate>
		<dc:creator>Andrew Harvey</dc:creator>
				<category><![CDATA[comp2121]]></category>
		<category><![CDATA[avr]]></category>
		<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://andrewharvey4.wordpress.com/?p=471</guid>
		<description><![CDATA[[Updated: Clarified what I wrote about the way stacks in AVR work, in the Push/Pop section.]
Data Transfer Instructions

(Credit: Hui Wu/COMP2121 Lecture Notes)
Load Direct (ld):
ld Rd, v
Rd  {r0, r1, ..., r31} and v  {x, x+, -x, y, y+, -y, z, z+, -z}
(remember the X, Y, Z pointers)
Load Program Memory (lpm):
Can take on three forms,



Syntax
Operation



lpm


R0⟵(Z)




lpm [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=471&subd=andrewharvey4&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><em>[Updated: Clarified what I wrote about the way stacks in AVR </em><em>work, in the Push/Pop section.</em><em>]</em></p>
<h2>Data Transfer Instructions</h2>
<p><a href="http://andrewharvey4.files.wordpress.com/2009/04/datatransfer.png"><img class="aligncenter size-full wp-image-474" title="datatransfer" src="http://andrewharvey4.files.wordpress.com/2009/04/datatransfer.png?w=450&#038;h=299" alt="datatransfer" width="450" height="299" /></a><strong></strong></p>
<p style="text-align:center;"><em>(Credit: Hui Wu/COMP2121 Lecture Notes)</em></p>
<p><strong>Load Direct (ld):</strong></p>
<pre>ld Rd, v
Rd <img src='http://l.wordpress.com/latex.php?latex=%5Cin&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='\in' title='\in' class='latex' /> {r0, r1, ..., r31} and v <img src='http://l.wordpress.com/latex.php?latex=%5Cin&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='\in' title='\in' class='latex' /> {x, x+, -x, y, y+, -y, z, z+, -z}</pre>
<p>(remember the <a href="http://andrewharvey4.wordpress.com/2009/04/13/comp2121-wk04/">X, Y, Z pointers</a>)</p>
<p><strong>Load Program Memory (lpm):</strong></p>
<p>Can take on three forms,</p>
<table style="height:132px;" border="0" width="153">
<tbody>
<tr>
<th>Syntax</th>
<th>Operation</th>
</tr>
<tr>
<td>
<pre style="text-align:left;">lpm</pre>
</td>
<td>
<pre style="text-align:center;">R0⟵(Z)</pre>
</td>
</tr>
<tr>
<td>
<pre style="text-align:left;">lpm Rd, Z</pre>
</td>
<td>
<pre style="text-align:center;">R0⟵(Z)</pre>
</td>
</tr>
<tr style="text-align:center;">
<td>
<pre style="text-align:left;">lpm Rd, Z+</pre>
</td>
<td>
<pre>Rd⟵(Z)
Z⟵Z+1</pre>
</td>
</tr>
</tbody>
</table>
<p>Z contains the byte address while the program flash memory uses word addressing. Therefore the word address must be converted into a byte address before having access to the data on the program flash memory. Example usage, (Table_1&lt;&lt;1 converts the word address into a byte address)</p>
<pre>   ldi zh, high(Table_1&lt;&lt;1) ; Initialise Z pointer
   ldi zl, low(Table_1&lt;&lt;1)
   lpm r16, z+ ; r16=0x76
   lpm r17, z ; r17=0x58
   ...
Table_1: .dw 0x5876
   ..<strong></strong></pre>
<p><strong>IN/OUT:</strong></p>
<p>AVR has 64 IO registers. Example,</p>
<pre style="padding-left:30px;">in Rd, A</pre>
<pre style="padding-left:30px;">out A, Rd</pre>
<pre style="padding-left:30px;">where 0 ≤ A ≤ 63.</pre>
<p><strong>Push/Pop:</strong></p>
<p>The stack pointer (implemented as two 8-bit registers in the I/O space) points to the next free space in RAM above/below (depends how you look at it) the stack. The stack in AVR is part of the SRAM space, and the stack (in AVR) grows from higher memory locations to lower memory locations. I&#8217;m talking about the actual value of the address, so 0xFFFF is a higher address than 0xDDDD. This got me a little confused at one stage because if you draw a picture of memory with 0&#215;0000 at the top of the diagram, and 0&#215;0001 below it and so on then in reference to the diagram a stack that is getting larger with PUSH operations is growing upwards (you usually associate higher to lower with down, this is why I got confused).</p>
<p>So the first thing you must do is initialise the stack pointer (RAMEND is a good starting location).</p>
<p>So a push operation,</p>
<pre>push Rr</pre>
<p>will push Rr onto the stack (ie. put the contents of Rr into the location that the SP points to), and then decrement the SP by 1. Pop has a similar opposite effect.</p>
<h2>Shift Instructions</h2>
<h3>Logical shift left</h3>
<pre>lsl Rd</pre>
<h3><a href="http://andrewharvey4.files.wordpress.com/2009/04/lsl.png"><img class="aligncenter size-full wp-image-483" title="lsl" src="http://andrewharvey4.files.wordpress.com/2009/04/lsl.png?w=450&#038;h=65" alt="lsl" width="450" height="65" /></a>Rotate Left Through Carry</h3>
<pre>rol Rd</pre>
<p><a href="http://andrewharvey4.files.wordpress.com/2009/04/rol.png"><img class="aligncenter size-full wp-image-484" title="rol" src="http://andrewharvey4.files.wordpress.com/2009/04/rol.png?w=450&#038;h=73" alt="rol" width="450" height="73" /></a></p>
<p>Both operation change some status register flags.</p>
<h2>Functions</h2>
<p>We dabbed into this in first year but just to revise and extend a little I&#8217;ll try to reiterate this here.</p>
<p>The <strong>heap</strong> is used for dynamic memory applications (eg. malloc()).</p>
<p>The <strong>stack</strong> is used to store return addresses, parameters, conflict registers and local variables and other things.</p>
<p>In passing parameters in WINAVR (C compiler for AVR) for say a function call they are passed <strong>by value for scalar variables</strong> (eg. char, int, float) and passed <strong>by reference for non-scalar variables</strong> (eg. array, struct).</p>
<p>Rules are needed between the caller and the callee to resolve issues such as,</p>
<ul>
<li>how to pass values and references to a function?</li>
<li>where to get the return value?</li>
<li>how to handle register conflicts? (if a function wants to use a register that was previously in use)</li>
<li>how to allocate and deallocate stack memory to and from local variables?</li>
</ul>
<p>If a register is used in both caller and callee and the caller needs its old value after the return from the callee, then a register conflict occurs. Either the compiler or the assembly programmer needs to check for this. The work around is to save the conflict registers on the stack.</p>
<p>The return value of a function needs to be stored in designated registers. WINAVR uses r25:r24 for this.</p>
<p>A stack consists of stack frames. A stack frame is created whenever a function is called, and it is freed whenever the function returns.<br />
<a href="http://andrewharvey4.files.wordpress.com/2009/04/avrstack.png"><img class="aligncenter size-full wp-image-472" title="avrstack" src="http://andrewharvey4.files.wordpress.com/2009/04/avrstack.png?w=450&#038;h=281" alt="avrstack" width="450" height="281" /></a></p>
<p style="text-align:center;"><em>(Credit: Hui Wu/COMP2121 Lecture Notes)</em></p>
<h2>Macros</h2>
<p>The AVR assembler offers macros. A macro is just a segment of code that you define and can then use by just calling the macro. Basically the macro name is just a place holder for the macro code. When the program is assembled the macro name will be replaced by the code that macro defines. This defines a macro named mymacro,</p>
<pre>.macro mymacro
 lds r2, @0
 lds r3, @1
 sts @1, r2
 sts @0, r3
.endmacro</pre>
<p>We can then invoke this macro with,</p>
<pre>mymacro p, q</pre>
<p>The p, q are used like arguments. So @0 will be replaced with p and @1 will be replaced by q. In AVR you can used @0 up to @9 in the macro body.</p>
<h2>Assembly Process</h2>
<p>The AVR assembler uses a two pass process.</p>
<p>Pass One:</p>
<ul>
<li>Lexical and syntax analysis: checking for syntax errors.</li>
<li>Record all symbols (labels&#8230;) in a <strong>symbol table</strong>.</li>
<li>Expand macro calls.</li>
</ul>
<p>Pass Two:</p>
<ul>
<li>Use symbol table to substitute the values for the symbols and evaluate functions (eg. low(-13167)).</li>
<li>Assemble each instruction (ie. generate machine code). For example add Rd, Rr is encoded as machine code as 0000 11rd dddd rrrr.</li>
</ul>
<h2>References</h2>
<p>Wu, Hui. <a href="http://mahler.cse.unsw.edu.au/webcms2/works/index.php?inc=LN&amp;cid=2050&amp;color=colour0"><em>COMP2121 09s1 Lecture Notes</em></a>.</p>
Posted in comp2121 Tagged: avr, comp2121, computing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrewharvey4.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrewharvey4.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrewharvey4.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrewharvey4.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrewharvey4.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrewharvey4.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrewharvey4.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrewharvey4.wordpress.com/471/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrewharvey4.wordpress.com/471/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrewharvey4.wordpress.com/471/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=471&subd=andrewharvey4&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://andrewharvey4.wordpress.com/2009/04/17/comp2121-wk0304-data-transferfunctions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/28043c88c4d0c2700ec793045d0ebe91?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">andrewharvey4</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/04/datatransfer.png" medium="image">
			<media:title type="html">datatransfer</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/04/lsl.png" medium="image">
			<media:title type="html">lsl</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/04/rol.png" medium="image">
			<media:title type="html">rol</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/04/avrstack.png" medium="image">
			<media:title type="html">avrstack</media:title>
		</media:content>
	</item>
		<item>
		<title>COMP2121 &#8211; ADD and ADC</title>
		<link>http://andrewharvey4.wordpress.com/2009/04/14/comp2121-add-and-adc/</link>
		<comments>http://andrewharvey4.wordpress.com/2009/04/14/comp2121-add-and-adc/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 02:23:21 +0000</pubDate>
		<dc:creator>Andrew Harvey</dc:creator>
				<category><![CDATA[comp2121]]></category>
		<category><![CDATA[avr]]></category>
		<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://andrewharvey4.wordpress.com/?p=465</guid>
		<description><![CDATA[Just some notes on using ADD and ADC (and also a not on compare and branch instructions at the end). (Thanks also to my lab partner who helped me with some of this stuff.)
The registers for AVR are 8 bits long, as noted if I want to store a 16 bit number I need to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=465&subd=andrewharvey4&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Just some notes on using ADD and ADC (and also a not on compare and branch instructions at the end). (Thanks also to my lab partner who helped me with some of this stuff.)</p>
<p>The registers for AVR are 8 bits long, as noted if I want to store a 16 bit number I need to use two registers, one will store the low byte and the other will store the high byte. eg the 16 bit binary number a = 1111111100000000 (as big endian) would be stored as a_high = 11111111, a_low = 00000000.</p>
<p>Another thing that I&#8217;ve learnt is how addition of multi byte numbers in AVR works. AVR has the instructions ADD (add without carry) and ADC (add with carry). Lets look at a simple case first.</p>
<pre>ldi r16, 0b10101010 //the 0b indicates binary ($ or 0x for hex, nothing for decimal, 0 for octal). loads the binary number 10101010 into register 16.
ldi r17, 0b10101010
add r16, r17</pre>
<p>What happens here is,</p>
<pre>  10101010
<span style="text-decoration:underline;">+ 10101010</span>
  01010100
  c c c c</pre>
<p>The carry bit is set, and hence and overflow has occurred. The actual answer is 101010100 but this has 9 bits and cannot fix in the 8 bits of space we have for the result to be stored into r16. Here we used the ADD instruction this means (at least to the best of my understanding, please correct me if I&#8217;m wrong) that we ignore what is originally in the carry flag (part of the status register). If we used ADC and the carry flag was set to 1 then when we add the right most bit as above 0+0 = 0 but we have a carry so we would get a 1 in that last bit. We can use this to do multi byte arithmetic.</p>
<p>Say we have,</p>
<pre>//a is a 16 bit number 1010101010101010
ldi al, 0b10101010
ldi ah, 0b10101010
//b is a 16 bit number 1010101010101010
ldi bl, 0b10101010
ldi bh, 0b10101010

//to do a+b (and store the result in a) we do
add al, bl
adc ah, bh</pre>
<p>We can then extend this to as many bytes as we want. Say we have a 3 byte number spread over the registers of a_0, a_1, a_2 and another 3 byte number in the registers b_0, b_1, b_2. We could add them like this,</p>
<pre>add a_0, b_0
adc a_1, b_1
adc a_2, b_2</pre>
<p>The result would be spread over a_0, a_1 and a_2 (remember though that we can still overflow this).</p>
<p>It was also enlightening (though also a nice reminder) to see how this low level addition works. (when doing 1bit addition (without carry), a + b, the sum = a xor b and the carry = a and b.) (see picture below)</p>
<p style="text-align:center;"><a href="http://andrewharvey4.files.wordpress.com/2009/04/onebitadderwithcarry.png"><img class="aligncenter size-full wp-image-466" title="onebitadder" src="http://andrewharvey4.files.wordpress.com/2009/04/onebitadder.png?w=450&#038;h=387" alt="onebitadder" width="450" height="387" /><img class="aligncenter size-full wp-image-467" title="onebitadderwithcarry" src="http://andrewharvey4.files.wordpress.com/2009/04/onebitadderwithcarry.png?w=450&#038;h=344" alt="onebitadderwithcarry" width="450" height="344" /></a>(Credit: Annie Guo, Basics of Digital Systems, COMP2121 Notes)</p>
<p>This concept can similarly be applied to other instructions. For example,</p>
<pre>cp a, b
breq label</pre>
<p>will compare a and be and branch to the label label if a is equal to b (and if not continue on executing the subsequent instructions). However what if you wanted to compare if a two byte number was equal to a two byte number? Sure you could just repeat the code one time for the low byte and another for the high byte but you can also do this,</p>
<pre>cp al, bl
cpc ah, bh
breq label</pre>
<p>These cp and cpc instructions use some trickery (well not really but I didn&#8217;t initially see how this worked, though its quite obvious now) where they modify some flags based on the result of the comparison and then the branch instructions (breq, brne, brlo&#8230;) look at the respective flag to see if they should branch or not.</p>
Posted in comp2121 Tagged: avr, comp2121, computing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrewharvey4.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrewharvey4.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrewharvey4.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrewharvey4.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrewharvey4.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrewharvey4.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrewharvey4.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrewharvey4.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrewharvey4.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrewharvey4.wordpress.com/465/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=465&subd=andrewharvey4&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://andrewharvey4.wordpress.com/2009/04/14/comp2121-add-and-adc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/28043c88c4d0c2700ec793045d0ebe91?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">andrewharvey4</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/04/onebitadder.png" medium="image">
			<media:title type="html">onebitadder</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/04/onebitadderwithcarry.png" medium="image">
			<media:title type="html">onebitadderwithcarry</media:title>
		</media:content>
	</item>
		<item>
		<title>COMP2121 &#8211; Wk04</title>
		<link>http://andrewharvey4.wordpress.com/2009/04/13/comp2121-wk04/</link>
		<comments>http://andrewharvey4.wordpress.com/2009/04/13/comp2121-wk04/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 23:18:26 +0000</pubDate>
		<dc:creator>Andrew Harvey</dc:creator>
				<category><![CDATA[comp2121]]></category>
		<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://andrewharvey4.wordpress.com/?p=450</guid>
		<description><![CDATA[Some general notes from an AVR Tutorial that I have found useful and need to reiterate.

&#8220;Only the registers from R16 to R31 load a constant immediately with the LDI command, R0 to R15 don&#8217;t do that.
The first register is always the target register where the result is written to!
Assembler directives always start with a dot [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=450&subd=andrewharvey4&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Some general notes from an AVR Tutorial that I have found useful and need to reiterate.</p>
<ul>
<li>&#8220;Only the registers from R16 to R31 load a constant immediately with the LDI command, R0 to R15 don&#8217;t do that.</li>
<li>The first register is always the target register where the result is written to!</li>
<li><span style="text-decoration:line-through;">Assembler directives always start with a dot in column 1 of the text. Instructions do NEVER start in column 1, they are always preceded by a Tab- or blank character!</span> [There are no restrictions with respect to column placement of labels, directives, comments or instructions.]</li>
<li>A very special extra role is defined for the register pairs R26:R27, R28:R29 and R30:R31. The role is so important that these pairs have extra names in AVR assembler: X, Y and Z. These pairs are 16-bit pointer registers, able to point to addresses with max. 16-bit into SRAM [(part of the data memory)] locations (X, Y or Z) or into locations in program memory (Z).<br />
The lower byte of the 16-bit-address is located in the lower register, the higher byte in the upper register. Both parts have their own names, e.g. the higher byte of Z is named ZH (=R31), the lower Byte is ZL (=R30). These names are defined in the standard header file for the chips. Dividing these 16-bit-pointer names into two different bytes is done like follows:</p>
<pre>.EQU Address = RAMEND ; RAMEND is the highest 16-bit address in SRAM
LDI YH,HIGH(Address) ; Set the MSB
LDI YL,LOW(Address) ; Set the LSB"¹</pre>
</li>
</ul>
<ul>
<li>Define names for registers with the .DEF directive, never use them with their direct name Rx.</li>
<li>If you need pointer access reserve R26 to R31 for that purpose.</li>
<li>16-bit-counter are best located R25:R24.</li>
<li>If you need to read from the program memory, e.g. fixed tables, reserve Z (R31:R30) and R0 for that purpose.</li>
<li>If you plan to have access to single bits within certain registers (e.g. for testing flags), use R16 to R23 for that purpose.¹</li>
</ul>
<p>Sections marked with ¹ are ©2002-2009 by <a href="http://www.avr-asm-tutorial.net/">http://www.avr-asm-tutorial.net</a>, from <em>Beginners Introduction to the Assembly Language of ATMEL AVR Microprocessors</em> by Gerhard Schmidt 2003. Used under the supplied license, &#8220;You may use, copy and distribute these pages as long as you keep the copyright information with it.&#8221;</p>
Posted in comp2121, computing Tagged: comp2121, computing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrewharvey4.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrewharvey4.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrewharvey4.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrewharvey4.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrewharvey4.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrewharvey4.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrewharvey4.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrewharvey4.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrewharvey4.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrewharvey4.wordpress.com/450/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=450&subd=andrewharvey4&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://andrewharvey4.wordpress.com/2009/04/13/comp2121-wk04/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/28043c88c4d0c2700ec793045d0ebe91?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">andrewharvey4</media:title>
		</media:content>
	</item>
		<item>
		<title>COMP2121 &#8211; Wk02</title>
		<link>http://andrewharvey4.wordpress.com/2009/03/20/comp2121-wk02/</link>
		<comments>http://andrewharvey4.wordpress.com/2009/03/20/comp2121-wk02/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 22:17:59 +0000</pubDate>
		<dc:creator>Andrew Harvey</dc:creator>
				<category><![CDATA[comp2121]]></category>
		<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://andrewharvey4.wordpress.com/?p=351</guid>
		<description><![CDATA[This week we learnt some of the things that separate assembly language from machine code in the context of AVR (or should I say AVR Studio).
Important Note 1: Assembly Language
In AVR assembly an input line takes the form of one of these, ([foo] = Optional)
[label:] directive [operands] [Comment]
[label:] instruction [operands] [Comment]
Comment
Empty Line
where a comment has [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=351&subd=andrewharvey4&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><span>This week we learnt some of the things that separate assembly language from machine code in the context of <span>AVR</span> (or should I say </span><a href="http://www.atmel.com/dyn/Products/tools_card.asp?tool_id=2725"><span><span>AVR</span> Studio</span></a>).</p>
<h3>Important Note 1: Assembly Language</h3>
<p><span>In <span>AVR</span> assembly an input line takes the form of one of these, ([foo] = Optional)</span></p>
<pre>[label:] directive [operands] [Comment]
[label:] instruction [operands] [Comment]
Comment
Empty Line</pre>
<p>where a comment has form,</p>
<pre>; [Text]</pre>
<h3>Important Note 2: Pseudo Instructions</h3>
<p><span><span>AVR</span> Assembly (using <span>AVR</span> studio) has some additional commands that are not part of the <span>AVR</span> instruction set, but the assembler (that is part of <span>AVR</span> studio) interprets into machine code.</span></p>
<p><span>Pseudo instructions are recognised by their preceding &#8216;.&#8217; (dot character). <span>eg</span>,</span></p>
<pre><span>.<span>equ</span> <span>CONST</span> = 31</span></pre>
<p><span>, will upon assembly go through the code and replace <span>CONST</span> with 31.</span></p>
<p><span>Here are the <span>AVR</span> assembly Pseudo Instructions.</span></p>
<table border="0">
<tbody>
<tr>
<th>Directive</th>
<th>Description</th>
</tr>
<tr>
<td>BYTE</td>
<td>Reserve byte to a variable</td>
</tr>
<tr>
<td><span><span>CSEG</span></span></td>
<td>Code Segment</td>
</tr>
<tr>
<td><span><span>CSEGSIZE</span></span></td>
<td>Program memory size</td>
</tr>
<tr>
<td>DB</td>
<td>Define constant byte(s)</td>
</tr>
<tr>
<td>DEF</td>
<td>Define a symbolic name on a register</td>
</tr>
<tr>
<td>DEVICE</td>
<td>Define which device to assemble for</td>
</tr>
<tr>
<td><span><span>DSEG</span></span></td>
<td>Data Segment</td>
</tr>
<tr>
<td><span><span>DW</span></span></td>
<td>Define Constant word(s)</td>
</tr>
<tr>
<td><span><span>ENDM</span>, <span>ENDMACRO</span></span></td>
<td>End macro</td>
</tr>
<tr>
<td><span><span>EQU</span></span></td>
<td>Set a symbol equal to an expression</td>
</tr>
<tr>
<td><span><span>ESEG</span></span></td>
<td><span><span>EEPROM</span> Segment</span></td>
</tr>
<tr>
<td>EXIT</td>
<td>Exit from file</td>
</tr>
<tr>
<td>INCLUDE</td>
<td>Read source from another file</td>
</tr>
<tr>
<td>LIST</td>
<td><span>Turn <span>listfile</span> generation on</span></td>
</tr>
<tr>
<td><span><span>LISTMAC</span></span></td>
<td>Turn Macro expansion in list file on</td>
</tr>
<tr>
<td>MACRO</td>
<td>Begin macro</td>
</tr>
<tr>
<td><span><span>NOLIST</span></span></td>
<td><span>Turn <span>listfile</span> generation off</span></td>
</tr>
<tr>
<td>ORG</td>
<td>Set program origin</td>
</tr>
<tr>
<td>SET</td>
<td>Set a symbol to an expression</td>
</tr>
</tbody>
</table>
<p><strong>.byte</strong><span> &#8211; Reserve some space (only allowed in <span>dseg</span>). <span>eg</span>.</span></p>
<pre><span>.<span>dseg</span></span>
  var1: .byte 4 ;reserve 4 bytes and store address in var1

.CSEG
  ldi r30, low(var1) ; Load address into Z low register
  ldi r31, high(var1) ; Load address into Z high register
  ld r1, Z ; Load VAR1 into register 1</pre>
<p>&#8230;and some more&#8230;</p>
<pre>.def FOO=r30 ;give register 30 the symbolic name FOO
<span>.<span>equ</span> var = 2 ;like C's #define statement</span>
.set var = 2 ;like a global variable in C</pre>
<h3>Important Note 3: Segments</h3>
<p><span>There <span>AVR</span> three segments of an <span>AVR</span> assembly program. Also when writing an assembly program you need to be able to specify which segment you are referring to, so you need to declare this using an <span>AVR</span> assembler directive shown in brackets below.</span></p>
<ol>
<li><span>Code Segment (.<span>cseg</span>)</span></li>
<li><span>Data Segment (.<span>dseg</span>)</span></li>
<li><span><span>EEPROM</span> Segment (.<span>eseg</span>)</span></li>
</ol>
<p>&#8220;The CSEG directive defines the start of a Code Segment. An Assembler file can consist of several Code Segments, which are concatenated into one Code Segment when assembled. The BYTE directive can not be used within a Code Segment. The default segment type is Code. The Code Segments have their own location counter which is a word counter. The ORG directive can be used to place code and constants at specific locations in the Program memory. The directive does not take any parameters.&#8221; [1]</p>
<h3>Notes from the Lab</h3>
<p><strong>Assembly Code and Machine Code</strong></p>
<p><span>In the lab we looked at this <span>AVR</span> assembly program,</span></p>
<pre>.include "m64def.inc"
.def a =r16 ; define a to be register r16
.def b =r17
.def c =r18
.def d =r19
.def e =r20
main: ; main is a label
<span><span>ldi</span> a, 10 ; load 10 into r16</span>
ldi b, -20
<span><span>mov</span> c, a ; copy the value of r16 into r18</span>
add c, b ; add r18 and r17 and store the result in r18
<span><span>mov</span> d, a</span>
sub d, b ; subtract r17 from r19 and store the result in r19
<span><span>lsl</span> c ; refer to <span>AVR</span> Instruction Set for the semantics of</span>
<span><span>asr</span> d ; <span>lsl</span> and <span>asr</span></span>
mov e, c
add e, d
loop:
inc r21 ; increase the value of r21 by 1
<span><span>rjmp</span> loop ; jump to loop</span></pre>
<p><span>The machine code executable produced by <span>AVR</span> studio was 24 bytes long, the question was why. It&#8217;s actually quite simple, all of the here instructions are 1 word (2 bytes = 16 bits), there are 12 instruction so total 24 bytes. One thing that initially confused me was the weird encoding. Back in 1917 I got the impression that if you have something like <span>mov</span> r16 r17 that this would be represented in machine code as some number for the <span>mov</span> operation followed by two more numbers of the same bit size for the registers. However this can vary depending on the architecture.</span></p>
<p><span>For example the <span>mov</span> operation, <span>MOV</span> Rd, <span>Rr</span> has encoding 0010 11rd <span>dddd</span> <span>rrrr</span>. The instruction takes up 6 bits, the source register takes up 5 bits and the destination takes up 5 bits (total 16 bits). The reason that the source and destination bits are intertwined is that it makes things easier on the hardware implementation to do it this way.</span></p>
<p>The program above has machine code (in hexadecimal),</p>
<pre><span>E00A EE1C 2F20 0F21 2F30 1B31 0F22 9535 2F42 0F43 5993 <span>CFFE</span></span></pre>
<p>and annotated,</p>
<pre><span>+00000000:   E00A        <span>LDI</span>     R16,0x0A         Load immediate</span>
+00000001:   EE1C        LDI     R17,0xEC         Load immediate
<span>+00000002:   2F20        <span>MOV</span>     R18,R16          Copy register</span>
+00000003:   0F21        ADD     R18,R17          Add without carry
<span>+00000004:   2F30        <span>MOV</span>     R19,R16          Copy register</span>
+00000005:   1B31        SUB     R19,R17          Subtract without carry
<span>+00000006:   0F22        <span>LSL</span>     R18              Logical Shift Left</span>
+00000007:   9535        ASR     R19              Arithmetic shift right
<span>+00000008:   2F42        <span>MOV</span>     R20,R18          Copy register</span>
+00000009:   0F43        ADD     R20,R19          Add without carry
+0000000A:   9553        INC     R21              Increment
<span>+0000000B:   <span>CFFE</span>        <span>RJMP</span>    PC-0x0001        Relative jump</span></pre>
<p><strong>Status Register<br />
</strong></p>
<p>Stepping through this program also shows a few of the status registers in use. The Status register, like all the other registers has 8 bits, namely,</p>
<table border="0">
<tbody>
<tr>
<th><span><span>SREG</span></span></th>
<th>Status Register</th>
</tr>
<tr>
<td>C</td>
<td>Carry Flag</td>
</tr>
<tr>
<td>Z</td>
<td>Zero Flag</td>
</tr>
<tr>
<td>N</td>
<td>Negative Flag</td>
</tr>
<tr>
<td>V</td>
<td>Two’s complement overflow indicator</td>
</tr>
<tr>
<td>S</td>
<td>N ⊕ V, For signed tests</td>
</tr>
<tr>
<td>H</td>
<td>Half Carry Flag</td>
</tr>
<tr>
<td>T</td>
<td><span>Transfer bit used by <span>BLD</span> and <span>BST</span> instructions</span></td>
</tr>
<tr>
<td>I</td>
<td>Global Interrupt Enable/Disable Flag</td>
</tr>
</tbody>
</table>
<p><span>Last week we saw how signed and unsigned numbers are stored, so if you look at the program above you will see that the last part just increments a register infinitely over and over. Stepping through this shows us what the status register does as we do this. Remember that <span>AVR</span> does all arithmetic in two&#8217;s compliment.</span></p>
<table border="0">
<tbody>
<tr>
<td>0</td>
<td>H Z</td>
</tr>
<tr>
<td>1-127</td>
<td>H</td>
</tr>
<tr>
<td>128</td>
<td>H V N</td>
</tr>
<tr>
<td>129-255</td>
<td>H S N</td>
</tr>
</tbody>
</table>
<p>As you can see the values that are negative under the two&#8217;s complement 128-255 have the N (negative) flag. Also from 127 then to 128 under two&#8217;s compliment we have gone from 127 to -128, so the V (Two’s complement overflow indicator) flag is indicated. 0 has the zero flag.</p>
<h3>The Rest</h3>
<p>The lecture notes go over some of the <a href="http://www.cse.unsw.edu.au/~cs2121/AVR/AVR-Instruction-Set.pdf"><span><span>AVR</span> instructions</span></a><span> but I think the docs provided by <span>Atmel</span> are fine. What I do think needs explaining (and me learning) is the carry flag and the difference between operations like add without carry (ADD) and add with carry (ADC).</span></p>
<p><span>Here is how I understand the carry bit. Say we have 4 bit registers and try to add (in binary) 1000 and 1000, the answer is 10000 however this is 5 bits and we only have 4 bits available to store the result. An overflow has occurred. To introduce some terminology, the most significant bit (or byte) (<span>msb</span>) is the left most bit (or byte) in big-<span>endian</span> (right most in little-<span>endian</span>), and vice-<span>versa</span> for least significant bit (or byte) (<span>lsb</span>). The carry bit (C flag) is the carry from the <span>msb</span>. This can indicate overflow in some cases but not always, it those cases the V flag (Two’s complement overflow indicator) is set.</span></p>
<p><span>So getting back to ADD and ADC, ADD will just add the two numbers ignoring the carry bit and ignoring overflow whereas ADC will actually add the carry bit to the result. At least this is what I&#8217;ve observed, though I&#8217;m not 100% sure on this.</span></p>
<h3><span>References</span></h3>
<p><span>[1] <a href="www.atmel.com/atmel/acrobat/doc1022.pdf">AVR Assembler User Guide</a>. </span>www.atmel.com/atmel/acrobat/doc1022.pdf</p>
<h3><span>Bibliography</span></h3>
<p><span><a href="www.atmel.com/atmel/acrobat/doc1022.pdf">AVR Assembler User Guide</a>. </span>www.atmel.com/atmel/acrobat/doc1022.pdf</p>
Posted in comp2121 Tagged: comp2121, computing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrewharvey4.wordpress.com/351/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrewharvey4.wordpress.com/351/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrewharvey4.wordpress.com/351/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrewharvey4.wordpress.com/351/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrewharvey4.wordpress.com/351/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrewharvey4.wordpress.com/351/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrewharvey4.wordpress.com/351/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrewharvey4.wordpress.com/351/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrewharvey4.wordpress.com/351/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrewharvey4.wordpress.com/351/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=351&subd=andrewharvey4&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://andrewharvey4.wordpress.com/2009/03/20/comp2121-wk02/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/28043c88c4d0c2700ec793045d0ebe91?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">andrewharvey4</media:title>
		</media:content>
	</item>
		<item>
		<title>COMP2121 &#8211; Wk01</title>
		<link>http://andrewharvey4.wordpress.com/2009/03/13/comp2121-wk01/</link>
		<comments>http://andrewharvey4.wordpress.com/2009/03/13/comp2121-wk01/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 23:48:08 +0000</pubDate>
		<dc:creator>Andrew Harvey</dc:creator>
				<category><![CDATA[comp2121]]></category>
		<category><![CDATA[computing]]></category>

		<guid isPermaLink="false">http://andrewharvey4.wordpress.com/?p=292</guid>
		<description><![CDATA[Just some reflections on the first week of my 09s1 COMP2121 Lectures and other materials. I will base a lot of this material on the course lecture notes by Hui Wu. Actually if you read this and the lecture notes you might see that they are pretty much identical.
The 4917 Microprocessor
This first lecture clarified a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=292&subd=andrewharvey4&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Just some reflections on the first week of my 09s1 COMP2121 Lectures and other materials. I will base a lot of this material on the course <a href="http://mahler.cse.unsw.edu.au/webcms2/works/index.php?inc=LN&amp;cid=2050&amp;color=colour0">lecture notes</a> by <a href="http://www.cse.unsw.edu.au/~huiw/">Hui Wu</a>. Actually if you read this and the lecture notes you might see that they are pretty much identical.</p>
<h2>The 4917 Microprocessor</h2>
<p>This first lecture clarified a lot of the things I&#8217;d learnt about low level computing in COMP1917, which was good. Back in COMP1917 we were introduced to the 4917 microprocessor (a hypothetical designed just for this course). It was 4bit, has 16 memory locations and 4 registers (instruction pointer, instruction store, general register 0 and general register 1). Each memory location could store a number between 0 and 15, and there were 16 instructions.</p>
<ul>
<li>1-byte Instructions
<ul>
<li>0 = Halt</li>
<li>1 = Add (R0 = R0 + R1)</li>
<li>2 = Subtract (R0 = R0 &#8211; R1)</li>
<li>3 = Increment R0 (R0 = R0 + 1)</li>
<li>4 = Increment R1 (R1 = R1 + 1)</li>
<li>5 = Decrement R0 (R0 = R0 &#8211; 1)</li>
<li>6 = Decrement R1 (R1 = R1 &#8211; 1)</li>
<li>7 = Ring Bell</li>
</ul>
</li>
<li>2-byte Instructions, value of the second byte is called &lt;data&gt;
<ul>
<li>8 = Print &lt;data&gt; (The numerical value of &lt;data&gt; is printed)</li>
<li>9 = Load value at address &lt;data&gt; into R0</li>
<li>10 = Load value at address &lt;data&gt; into R1</li>
<li>11 = Store R0 into address &lt;data&gt;</li>
<li>12 = Store R1 into address &lt;data&gt;</li>
<li>13 = Jump to address &lt;data&gt;</li>
<li>14 = Jump to address &lt;data&gt; if R0 == 0</li>
<li>15 = Jump to address &lt;data&gt; if R0 != 0</li>
</ul>
</li>
</ul>
<p>It had a simple start up (registers set to 0, all memory locations set to 0, fetch-execute cycle begins), and a fetch-execute cyle,</p>
<ul>
<li>The instruction at the address given by the instruction pointer is loaded into the instruction store.</li>
<li>The instruction pointer is incremented by 1 or 2 depending on whether the instruction store is a 1 or 2 byte instruction.</li>
<li>The instruction in the instruction store is executed.</li>
<li>This is repeated until the instruction store equals 0 (HALT)</li>
</ul>
<p>So for example the following 4917 machine code program would print 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15.</p>
<pre>8 0 3 11 1 15 0</pre>
<p>This is understandable but was difficult for me to tie into my desktop computer level, for instance the CPU and the RAM. This became a bit clearer today.</p>
<h2>The Von Newman and The Harvard Architectures</h2>
<p>Two main computer architectures include the Von Newman Architecture and the Harvard architecture. The hypothetical 4917 from COMP1917 was a <strong>Von Newman Architecture</strong> because both the program and the data were stored in the same memory. (It seems that processors like the Intel Pentium 4, Intel Core 2, etc. use this architecture)</p>
<p><a href="http://andrewharvey4.files.wordpress.com/2009/03/vonnewman.png"><img class="aligncenter size-full wp-image-294" title="vonnewman" src="http://andrewharvey4.files.wordpress.com/2009/03/vonnewman.png?w=384&#038;h=240" alt="vonnewman" width="384" height="240" /></a></p>
<p>Another key thing is the Arithmetic and Logic Unit (ALU) and the Control Unit are collectively called the CPU (Central Processing Unit). A microprocessor is just a CPU on a single chip. A microcontroller is basically a computer on a chip, so the CPU, memory, etc. are all on a single chip.</p>
<p>I learnt a lot from experimenting with this architecture and the 4917 (and its successors). Though when I began to write more complex programs I found myself constantly putting a GOTO instruction number <em>n</em> command at the very begining, and using that skiped space as memory for data. I also came to see that this architecture allows for the buffer overflow vulnerability as program data can be executed as instructions if there are vulnerability. These two observations tend to lead to the Harvard architecture (which I am new to).</p>
<p><a href="http://andrewharvey4.files.wordpress.com/2009/03/harvard.png"><img class="aligncenter size-full wp-image-297" title="harvard" src="http://andrewharvey4.files.wordpress.com/2009/03/harvard.png?w=384&#038;h=240" alt="harvard" width="384" height="240" /></a></p>
<p>This is the architecture used for the <a href="http://en.wikipedia.org/wiki/Atmel_AVR">Atmel AVR microprocessor</a>, which is what we will focus on in this course (I think). I&#8217;ll come back to this when I talk about address space.</p>
<h2>Computer Memory</h2>
<p>This is the computer memory hierarchy diagram from the lecture notes.</p>
<p><a href="http://andrewharvey4.files.wordpress.com/2009/03/mem_hierarchy.png"><img class="aligncenter size-full wp-image-298" title="mem_hierarchy" src="http://andrewharvey4.files.wordpress.com/2009/03/mem_hierarchy.png?w=428&#038;h=319" alt="mem_hierarchy" width="428" height="319" /></a></p>
<p>This helps put a lot of my misunderstandings of how the 4917 from COMP1917 relates to modern processors, as I didn&#8217;t quite see if the instructions and data were stored in RAM or on the CPU. But really this is just an implementation issue so it didn&#8217;t really matter to the 4917.</p>
<p>In the CPU there are <a href="http://en.wikipedia.org/wiki/Processor_register">registers</a> which are really fast, but there are few (apparently in x86 (eg. Pentium, Core 2) there are only 8 integer and 8 floating point registers). Then there is the cache (on chip memory), this is what that 4MB cache that I see advertised that my CPU has. This cache can be separated for program code and data. This is faster that reading from RAM (the off chip memory), so currently active program code is moved to here from the RAM when necessary to speed things up (this is apparently implemented on the hardware level (which is always nice for a software developer <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ). Then there is off-chip memory and auxiliary storage. This fits in nicely with the picture I get when I open up my computer.</p>
<p>The material on the types of RAM and ROM in the lecture notes needs no further commentary, so I&#8217;ll skip that part.</p>
<h2>ISA (Instruction Set Architecture)</h2>
<p>&#8220;ISA is the interface between hardware and software</p>
<ul>
<li>For (machine language) programmers (and compiler writers)
<ul>
<li>Don’t need to know (much) about microarchitecture</li>
<li>Just write or generate instructions that match the ISA</li>
</ul>
</li>
<li>For hardware (microarchitecture) designers
<ul>
<li>Don’t need to know about the high level software</li>
<li>Just build a microarchitecture that implements the ISA&#8221; &#8211;Wu</li>
</ul>
</li>
</ul>
<p>This sums up the situation nicely, and makes perfect sense.</p>
<p>There are 4 things that make up memory instruction set architectures,</p>
<ul>
<li>Memory Models (how does the memory look to the CPU)
<ul>
<li>Addressable cell size. Most commonly is 8 bits (= 1 byte) (though AVR uses 16 bits for program memory)</li>
<li>Alignment</li>
<li>Address Space (AVR address space shown below)<br />
<a href="http://andrewharvey4.files.wordpress.com/2009/03/avr_address_spaces.png"><img class="size-full wp-image-316 alignnone" title="avr_address_spaces" src="http://andrewharvey4.files.wordpress.com/2009/03/avr_address_spaces.png?w=450&#038;h=262" alt="avr_address_spaces" width="450" height="262" /></a></li>
<li>Endianness (Least or most significant byte first)</li>
</ul>
</li>
<li>Registers
<ul>
<li>General Purpose (temp results&#8230;)</li>
<li>Special Purpose
<ul>
<li>Program Counter (PC)</li>
<li>Stack Pointer (SP)</li>
<li>Input/Output Registers</li>
<li>Status Registers</li>
</ul>
</li>
</ul>
</li>
<li>Data Types</li>
<li>Instructions</li>
</ul>
<p>RISC &#8211; Reduced Instruction Set Computer<br />
CISC &#8211; Complex Instruction Set Computer</p>
<p>Atmel AVR is RISC.</p>
<h2>Number Systems</h2>
<p>Problem: How do we represent <strong>negative numbers</strong> in a computer? 4 main methods (<a href="http://en.wikipedia.org/w/index.php?title=Signed_number_representations">from Wikipedia</a>).</p>
<ul>
<li> Sign-and-magnitude &#8211; Allocate one bit as the sign (problems: two zeros, circuitry more complicated)</li>
<li>Ones&#8217; complement &#8211; (problems: two zeros)</li>
</ul>
<p style="text-align:left;"><a href="http://andrewharvey4.files.wordpress.com/2009/03/ones_compliment.png"><img class="size-full wp-image-341 aligncenter" title="ones_compliment" src="http://andrewharvey4.files.wordpress.com/2009/03/ones_compliment.png?w=180&#038;h=302" alt="ones_compliment" width="180" height="302" /></a></p>
<ul>
<li>Two&#8217;s complement</li>
</ul>
<p style="text-align:left;"><a href="http://andrewharvey4.files.wordpress.com/2009/03/twos_compliment.png"><img class="size-full wp-image-342 aligncenter" title="twos_compliment" src="http://andrewharvey4.files.wordpress.com/2009/03/twos_compliment.png?w=180&#038;h=278" alt="twos_compliment" width="180" height="278" /></a></p>
<ul>
<li> Excess-<em>N </em>(not in lecture notes)</li>
</ul>
<p>It is important to know that the hardware does all arithmetic in 2&#8217;s compliment. It is up to the programmer to interpret the number as signed or unsigned.</p>
<p>To convert a number from 2&#8217;s compliment, for example -45 in 2&#8217;s compliment is 11010011, we can do something like this,</p>
<p><img src='http://l.wordpress.com/latex.php?latex=1+%5Ctimes+%28-2%5E7%29+%2B+1+%5Ctimes+2%5E6+%2B+1+%5Ctimes+2%5E6+%2B+0+%5Ctimes+2%5E5+%2B1+%5Ctimes+2%5E4+%2B+0+%5Ctimes+2%5E3+%2B+0+%5Ctimes+2%5E2+%2B+1+%5Ctimes+2%5E1+%2B+1+%5Ctimes+2%5E0+%3D+-45&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='1 \times (-2^7) + 1 \times 2^6 + 1 \times 2^6 + 0 \times 2^5 +1 \times 2^4 + 0 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = -45' title='1 \times (-2^7) + 1 \times 2^6 + 1 \times 2^6 + 0 \times 2^5 +1 \times 2^4 + 0 \times 2^3 + 0 \times 2^2 + 1 \times 2^1 + 1 \times 2^0 = -45' class='latex' /></p>
<p>To go the other way from say -1 to the 2&#8217;s compliment form 11111111 we use that <img src='http://l.wordpress.com/latex.php?latex=2%5Ep+-+X&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^p - X' title='2^p - X' class='latex' /> formula. I&#8217;m not exactly sure how its supposed to work so I&#8217;ve hacked it to make it work.</p>
<p>If the number you wish to convert is negative, let <img src='http://l.wordpress.com/latex.php?latex=X+%3D+-n&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='X = -n' title='X = -n' class='latex' />, so that X is positive then take <img src='http://l.wordpress.com/latex.php?latex=2%5Ep&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^p' title='2^p' class='latex' /> where <em>p</em> is the number of bits you are using (say 8), then subtract X. If the number to convert is less than <img src='http://l.wordpress.com/latex.php?latex=2%5Ep&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^p' title='2^p' class='latex' /> (where <em>p</em> is the number of bits, say 8 ) then leave it as is and that in your 2&#8217;s compliment.</p>
<p>Now that was complicated. But its the only way I can get that advertised <img src='http://l.wordpress.com/latex.php?latex=2%5Ep+-+X&#038;bg=ffffff&#038;fg=333333&#038;s=0' alt='2^p - X' title='2^p - X' class='latex' /> formula to work with the given set of sample data (as in that table above).</p>
<p>This raises an issue for comparison operations. eg. Is 1111 1100<sub>two</sub> &gt; 0000 0010<sub>two</sub>? If those two numbers are unsigned then YES, if they are signed thin NO. As such the hardware uses the S flag in AVR to indicate the result of the signed comparison, and the C flag to indicate the result of unsigned comparison.</p>
<p style="text-align:left;"><strong>Representing Strings.</strong> We saw one method back in COMP1917 though its nice to see that the other methods that come to mind were used also.</p>
<ol>
<li>1st position of string reserved for length (Pascal)</li>
<li>an accompanying variable has the length of the string (as in a structure)</li>
<li>last position of string marked with a special character (NULL in C)</li>
</ol>
<h2>Sign Extension</h2>
<p>How do we extend a binary number of <em>m</em> bits to an equivalent binary number of <em>m</em> + <em>n</em> bits? If the number is positive add <em>n</em> 0&#8217;s to the most significant side (usually left) of the binary number. If the number is negative add <em>n</em> 1&#8217;s to the most significant side of the binary number. This is called sign extension. To add twe binary numbers of different lengths just sign extend the shorter one so that both numbers have the same bit length then add as usual.</p>
<h2>References</h2>
<p>Wu, Hui. <em>COMP2121 09s1 Lecture Notes</em>.</p>
<p><em>(Updated 20th June 2009 with explanation of 2&#8217;s compliment conversions and sign extension)</em></p>
Posted in comp2121, computing Tagged: comp2121, computing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrewharvey4.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrewharvey4.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrewharvey4.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrewharvey4.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrewharvey4.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrewharvey4.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrewharvey4.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrewharvey4.wordpress.com/292/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrewharvey4.wordpress.com/292/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrewharvey4.wordpress.com/292/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrewharvey4.wordpress.com&blog=5148276&post=292&subd=andrewharvey4&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://andrewharvey4.wordpress.com/2009/03/13/comp2121-wk01/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/28043c88c4d0c2700ec793045d0ebe91?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=PG" medium="image">
			<media:title type="html">andrewharvey4</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/03/vonnewman.png" medium="image">
			<media:title type="html">vonnewman</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/03/harvard.png" medium="image">
			<media:title type="html">harvard</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/03/mem_hierarchy.png" medium="image">
			<media:title type="html">mem_hierarchy</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/03/avr_address_spaces.png" medium="image">
			<media:title type="html">avr_address_spaces</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/03/ones_compliment.png" medium="image">
			<media:title type="html">ones_compliment</media:title>
		</media:content>

		<media:content url="http://andrewharvey4.files.wordpress.com/2009/03/twos_compliment.png" medium="image">
			<media:title type="html">twos_compliment</media:title>
		</media:content>
	</item>
	</channel>
</rss>