<?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/"
	>

<channel>
	<title>ARM &#8211; IoT Expert</title>
	<atom:link href="https://iotexpert.com/tag/arm/feed/" rel="self" type="application/rss+xml" />
	<link>https://iotexpert.com</link>
	<description>Engineering for the Internet of Things</description>
	<lastBuildDate>Fri, 09 Feb 2018 22:32:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://iotexpert.com/wp-content/uploads/2017/01/cropped-Avatar-32x32.jpg</url>
	<title>ARM &#8211; IoT Expert</title>
	<link>https://iotexpert.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>The Lost Art of Assembly Language Programming</title>
		<link>https://iotexpert.com/lost-art-assembly-language-programming/</link>
					<comments>https://iotexpert.com/lost-art-assembly-language-programming/#comments</comments>
		
		<dc:creator><![CDATA[Darrin Vallis]]></dc:creator>
		<pubDate>Fri, 09 Feb 2018 19:53:37 +0000</pubDate>
				<category><![CDATA[CY8CKIT-042]]></category>
		<category><![CDATA[CY8CKIT-044]]></category>
		<category><![CDATA[Cy8CKIT-145]]></category>
		<category><![CDATA[Embedded Design]]></category>
		<category><![CDATA[PSoC 4000S]]></category>
		<category><![CDATA[PSoC 4200]]></category>
		<category><![CDATA[PSoC Creator]]></category>
		<category><![CDATA[ARM]]></category>
		<category><![CDATA[Assembly. Machine Code]]></category>
		<guid isPermaLink="false">https://iotexpert.com/?p=4992</guid>

					<description><![CDATA[Cypress introduced it&#8217;s first mass market microcontroller in 2001. It used a Cypress designed 8 bit CISC processor running at 24 MHz, with as little as 4 KB Flash and 256 bytes RAM. Wrapped around that was a neat array of programmable analog and digital blocks. This may not sound like much, but with a [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Cypress introduced it&#8217;s first mass market microcontroller in 2001. It used a Cypress designed 8 bit CISC processor running at 24 MHz, with as little as 4 KB Flash and 256 bytes RAM. Wrapped around that was a neat array of programmable analog and digital blocks. This may not sound like much, but with a creative mindset you could get these parts to do amazing things. For instance, I once implemented a complete ultrasonic ranging sensor with full wave analog demodulation in a single PSOC1 as shown below.</p>
<figure id="attachment_4993" aria-describedby="caption-attachment-4993" style="width: 1024px" class="wp-caption aligncenter"><a href="https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1.jpg"><img fetchpriority="high" decoding="async" width="1024" height="430" class="size-large wp-image-4993" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-1024x430.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-1024x430.jpg 1024w, https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-600x252.jpg 600w, https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-300x126.jpg 300w, https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1-768x323.jpg 768w, https://iotexpert.com/wp-content/uploads/2018/02/UltrasonicRangingPSOC1.jpg 1040w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><figcaption id="caption-attachment-4993" class="wp-caption-text">PSOC1 Ultrasonic Ranging</figcaption></figure>
<p>With CPU resources at a premium, you had to write tight, efficient code to get the most out of PSOC1. A single C library could consume the entire Flash. Consequently, I wrote a <em>lot</em> of assembly code. That&#8217;s not so bad, since I actually enjoy it more than C. There&#8217;s a certain elegance to well written, fully commented machine code. In the case of PSOC1, here&#8217;s what you had to work with: 5 registers, some RAM and Flash. That&#8217;s it. Real Men Write In Assembly.</p>
<figure id="attachment_4995" aria-describedby="caption-attachment-4995" style="width: 518px" class="wp-caption aligncenter"><a href="https://iotexpert.com/wp-content/uploads/2018/02/M8Carch.jpg"><img decoding="async" width="518" height="341" class="size-full wp-image-4995" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/M8Carch.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/M8Carch.jpg 518w, https://iotexpert.com/wp-content/uploads/2018/02/M8Carch-300x197.jpg 300w" sizes="(max-width: 518px) 100vw, 518px" /></a><figcaption id="caption-attachment-4995" class="wp-caption-text">M8C Architecture</figcaption></figure>
<p>&nbsp;</p>
<p>We&#8217;ll start with simple machine code instruction to make the CPU do something. You can reference the M8C assembly language user guide <a href="http://www.cypress.com/file/72341/download">here</a> for more details. To get the M8C to execute 2+3=5 we write:</p>
<p><span style="font-family: Courier New;color: black">mov A,2       ;Load A with 2<br />
add A,3       ;Add 3 to A. Result=5 is in A<br />
</span></p>
<p>We can get fancy by using variables. Let&#8217;s add R=P+Q. Assume P is at RAM location 0x20 and Q is at location 0x21, and R is at 0x22</p>
<p><span style="font-family: Courier New;color: black">;Initialize variables<br />
mov [0x20],2  ;Load P with 2<br />
mov [0x21],3  ;Load Q with 3</span></p>
<p><span style="font-family: Courier New;color: black">;Add variables<br />
mov X,[0x20]  ;X &lt;- P<br />
mov A,[0x21]  ;A &lt;- Q<br />
adc [X],A     ;X &lt;- P + Q<br />
mov [0x22],X  ;R &lt;- X<br />
</span><br />
The fun thing about assembly is you can always dream up cool ways of doing things in less operations based on the machine&#8217;s instruction set. For example, we can simplify the above code as follows:</p>
<p><span style="font-family: Courier New;color: black">;Add variables<br />
mov [0x20],[0x22]   ;R &lt;- P<br />
adc [0x22],[0x21]   ;R &lt;- P + Q</span></p>
<p>In my experience, a good programmer with expert knowledge of the instruction set and CPU resources can always write better code than a compiler. There&#8217;s a certain human creativity that algorithms can&#8217;t match.</p>
<p>All that being said, I had not seen a good &#8220;machine code 101&#8221; tutorial for writing assembly in PSOC Creator on modern ARM M0 processors. So let&#8217;s walk through one now. We&#8217;ll use a CY8CKIT-145 and blink the LED. It&#8217;s just what happens to be laying around on the lab bench. Any PSOC4 kit will do.</p>
<p><img decoding="async" width="518" height="341" class="size-full wp-image-4995 aligncenter" alt="CY8CKIT-145-40XX" src="http://www.cypress.com/sites/default/files/media-embed/263821/CY8CKIT-145_image.png" /></p>
<p>We&#8217;ll start by creating a standard project in PSOC Creator, drop a Digital Output pin on the schematic and call it &#8220;LED&#8221;</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/1.jpg"><img loading="lazy" decoding="async" width="1023" height="585" class="alignnone wp-image-5014 size-full" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/1.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/1.jpg 1023w, https://iotexpert.com/wp-content/uploads/2018/02/1-600x343.jpg 600w, https://iotexpert.com/wp-content/uploads/2018/02/1-300x172.jpg 300w, https://iotexpert.com/wp-content/uploads/2018/02/1-768x439.jpg 768w" sizes="auto, (max-width: 1023px) 100vw, 1023px" /></a></p>
<p>Then open the .CYDWR file and drag pin LED to P2[5], since that&#8217;s where it is on the LED board. Yours may be in a different place on whatever board you are using.</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/2.jpg"><img loading="lazy" decoding="async" width="871" height="579" class="alignnone size-full wp-image-5015" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/2.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/2.jpg 871w, https://iotexpert.com/wp-content/uploads/2018/02/2-600x399.jpg 600w, https://iotexpert.com/wp-content/uploads/2018/02/2-300x199.jpg 300w, https://iotexpert.com/wp-content/uploads/2018/02/2-768x511.jpg 768w" sizes="auto, (max-width: 871px) 100vw, 871px" /></a></p>
<p>Now under &#8220;Source Files&#8221; in the workspace directory you will delete main.c and replace with main.s</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/3JPG.jpg"><img loading="lazy" decoding="async" width="343" height="567" class="alignnone size-full wp-image-5016" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/3JPG.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/3JPG.jpg 343w, https://iotexpert.com/wp-content/uploads/2018/02/3JPG-181x300.jpg 181w" sizes="auto, (max-width: 343px) 100vw, 343px" /></a></p>
<p>Now right clock on &#8220;Source Files&#8221;, select &#8220;Add New File&#8221; and select &#8220;GNU ARM Assembly File&#8221; in the dialog. Rename the file from GNUArmAssembly01.s to main.s</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/4.jpg"><img loading="lazy" decoding="async" width="524" height="402" class="alignnone size-full wp-image-5017" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/4.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/4.jpg 524w, https://iotexpert.com/wp-content/uploads/2018/02/4-300x230.jpg 300w" sizes="auto, (max-width: 524px) 100vw, 524px" /></a></p>
<p>Your workspace ends up looking like this:</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/5.jpg"><img loading="lazy" decoding="async" width="319" height="339" class="alignnone size-full wp-image-5018" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/5.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/5.jpg 319w, https://iotexpert.com/wp-content/uploads/2018/02/5-282x300.jpg 282w" sizes="auto, (max-width: 319px) 100vw, 319px" /></a></p>
<p>So far, so good. Now open main.s, delete everything if it&#8217;s not empty and add the following code. This sets up the IDE for M0 assembly architecture</p>
<p><span style="font-family: Courier New;color: black">// ==============================================<br />
// ARM M0 Assembly Tutorial<br />
//<br />
// 01 &#8211; Blink LED<br />
// ==============================================<br />
.syntax unified<br />
.text<br />
.thumb<br />
</span><br />
Next we need to include register definitions for the chip we are using. These are all from the PSOC4 Technical Reference Manual (TRM)</p>
<p><span style="font-family: Courier New;color: black">// ==============================================<br />
// Includes<br />
// ==============================================<br />
.include &#8220;cydevicegnu_trm.inc&#8221;<br />
</span></p>
<p>Then we are going to do some .equ statements, same as #define in C. This identifies the Port 2 GPIO data register plus bits for the LED pin in on and off state</p>
<p><span style="font-family: Courier New;color: black">// ==============================================<br />
// Defines<br />
// ==============================================<br />
.equ LED_DR,CYREG_GPIO_PRT2_DR          // LED data reg address<br />
.equ LED_PIN,5                          // P2.5<br />
.equ LED_OFF,1&lt;&lt;led_pin                 // 0010 0000<br />
.equ LED_ON,~LED_OFF                    // 1101 1111<br />
</span></p>
<p>Now you add the right syntax to set up main()</p>
<p><span style="font-family: Courier New;color: black">// ==============================================<br />
// main<br />
// ==============================================<br />
.global main<br />
.func main, main<br />
.type main, %function<br />
.thumb_func<br />
</span></p>
<p>Finally we add the code for main, which is pretty simple:</p>
<p><span style="font-family: Courier New;color: black">main:<br />
ldr r5,=LED_DR      // Load GPIO port addr to r5</span></p>
<p><span style="font-family: Courier New;color: black">loop0:<br />
ldr r6,=LED_ON      // Move led data to r6<br />
str r6,[r5]         // Write r6 data to r5 addr</span></p>
<p><span style="font-family: Courier New;color: black">ldr r0,=0xFFFFFF    // Argument passed in r0<br />
bl CyDelayCycles    // Delay for N cycles</span></p>
<p><span style="font-family: Courier New;color: black">ldr r6,=LED_OFF     // Move led data to r6<br />
str r6,[r5]         // Write r6 data to r5 addr</span></p>
<p><span style="font-family: Courier New;color: black">ldr r0,=0xFFFFFF    // Argument passed in r0<br />
bl CyDelayCycles    // Delay for N cycles</span></p>
<p><span style="font-family: Courier New;color: black">b loop0             // Branch loop0</span></p>
<p><span style="font-family: Courier New;color: black">.endfunc            // End of main<br />
.end                // End of code<br />
</span></p>
<p>One thing to note: The function CyDelayCycles is defined CyBootAsmGnu.s. Any function in assembly gets its arguments passed by the first 4 registers r0,r1,r2 and r3. Before calling the function you simply load r0 with the argument then do a bl (branch with link). This is also why I avoided the first 4 registers when messing with LED data. If you&#8217;re interested in doing more with ARM assembly, definitely read the <a href="https://iotexpert.com/wp-content/uploads/2018/02/m0p_trm.pdf">Cortex M0+ Technical Reference Manual</a>. It&#8217;s a great primer for the M0+ instruction set.</p>
<p>That&#8217;s it. End result is a blinking LED. Cool thing is you can use PSOC Creator with all it&#8217;s nice features, but sill access the power of machine code.</p>
<p>You can get the project ZIP file <a href="https://iotexpert.com/wp-content/uploads/2018/02/M0_ASM_v2.zip">here</a>.</p>
<p>Regards<br />
Darrin Vallis</p>
<p><a href="https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339.jpg"><img loading="lazy" decoding="async" width="1024" height="768" class="alignnone size-large wp-image-5022" alt="" src="https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-1024x768.jpg" srcset="https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-1024x768.jpg 1024w, https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-600x450.jpg 600w, https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-300x225.jpg 300w, https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339-768x576.jpg 768w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a><a href="https://iotexpert.com/wp-content/uploads/2018/02/M0_ASM_v2.zip"></a><a href="https://iotexpert.com/wp-content/uploads/2018/02/20180209_133339.jpg"></a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://iotexpert.com/lost-art-assembly-language-programming/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
