We've detected that you're using an ad content blocking browser plug-in or feature. Ads provide a critical source of revenue to the continued operation of Silicon Investor.  We ask that you disable ad blocking while on Silicon Investor in the best interests of our community.  If you are not using an ad blocker but are still receiving this message, make sure your browser's tracking protection is set to the 'standard' level.

   Technology StocksNew Technology

Previous 10 
From: FJB6/16/2021 2:10:59 PM
   of 407

Code in Assembly for Apple Silicon with the AsmAttic app

Learning a little assembly language is not only good for the soul, but it has value for anyone wanting to deepen their understanding of a processor, those who want to read disassembled code such as security researchers, and anyone writing code in a higher-level language such as Objective-C or Swift. Although there are several good books about ARM assembly (see references), using it in Xcode apps is not well documented. Apple’s developer information is most helpful to those who already write assembly and are accustomed to its quirks.

This article is the first of what I hope will be a series to open up access to assembly coding for Apple Silicon Macs. Here I take you through building a simple app which wraps itself around four lines of ARM64 assembly code, and provides a platform for subsequent articles. To work through this, you’ll need an M1 Mac and Xcode 12.5 (free from the App Store), and I assume that you’re sufficiently familiar with that and Swift to be able to build a basic app in AppKit or similar.

The complete Xcode project is available here: asmattic

Start by creating a new project for a macOS app, which I’ve named AsmAttic. Set its Interface and Life Cycle to support your favourite model. In my case, that’s a conventional Storyboard with an AppKit App Delegate and Swift as its language. You’re welcome to use SwiftUI or anything else which you find straightforward.

In its Project Build Settings, set it to build the Active Architecture only for both debug and release versions, so that Xcode will only make ARM versions of the app. If you want to support Intel, you’ll need to add conditionals to ensure that the assembly is only built for and called by the ARM version.

Build your app a little interface window, here with three numeric input boxes, and a scrolling text output view, so that it can take three floating point numbers as input, and write a string containing the results. Add real code to perform the compiled equivalent of what you’re going to code in assembly, a double multiply and add.

My primary purpose in exploring ARM assembly is to look in greater detail at its floating point arithmetic. The instructions which I’m most interested in merge two arithmetic operations, multiply and add. They take three doubles, a, b and c, and calculate the result of
(a * b) + c
They’re of particular interest to me because they reduce error compared with two separate operations. So that’s what this initial version of AsmAttic is going to perform in both Swift and assembly.

At this stage, wire up the window with code which performs that using Swift (see the completed code below for one solution). Test the app to ensure that it works without calling any assembly routines.

When you’re happy that’s working correctly, add a New File, selecting the Assembly type, and naming it asmmath.s. The code that contains is short and sweet:
.global _multadd
.align 4

STR LR, [SP, #-16]!
FMADD D0, D0, D1, D2
LDR LR, [SP], #16

That stores a set of registers, performs the FMADD operation on the three doubles, leaves the result in D0, restores the registers, and returns.

To be able to access that from Swift, you then need a C header file, asmmath.h, which contains just the following:
#ifndef asmmath_h
#define asmmath_h
extern double multadd(double, double, double);
#endif /* asmmath_h */
As with the other files here, ensure this is added to the target of the project.

In theory, Xcode should automatically generate a bridging header to enable your Swift code to call that assembly routine. In practice, I’ve not seen that happen, and have had to create that manually. To do that, add another Header file, this time named AsmAttic-Bridging-Header.h. Inside that, the key line is:
#include "asmmath.h"
which bridges between Swift and the C header, which in turn wraps the _multadd routine in assembly language.

The final step is to tell Xcode to use that bridging header in the project’s Build Settings. Locate within those the Swift Compiler – General section, and add as the location for the bridging header the path to your file relative to the project file, typically something like
If you get that wrong, Xcode will complain that it can’t find the bridging header and builds will fail with that error.

Go back to your Swift code to handle the button press, and call the assembly routine using code such as
let theRes2 = multadd(theA.doubleValue, theB.doubleValue, theC.doubleValue)
so you can print theRes2 as its result in the output text.

My final code for the ViewController reads:

class ViewController: NSViewController { @IBOutlet weak var no1Text: NSTextField! @IBOutlet weak var no1Formatter: NumberFormatter! @IBOutlet weak var no2Text: NSTextField! @IBOutlet weak var no2Formatter: NumberFormatter! @IBOutlet weak var no3Text: NSTextField! @IBOutlet weak var no3Formatter: NumberFormatter! @IBOutlet var outputText: NSTextView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override var representedObject: Any? { didSet { // Update the view, if already loaded. } } @IBAction func goButton(_ sender: Any) { if let theA = self.no1Formatter.number(from: self.no1Text.stringValue) { if let theB = self.no2Formatter.number(from: self.no2Text.stringValue) { if let theC = self.no3Formatter.number(from: self.no3Text.stringValue) { let theRes1 = theA.doubleValue * theB.doubleValue + theC.doubleValue let theRes2 = multadd(theA.doubleValue, theB.doubleValue, theC.doubleValue) self.outputText.string = "In Swift \(theRes1), by assembler \(theRes2)\n" } } } } } Your app should now let you set the three variables, calculate the result both using Swift and the FMADD operation, and write the result to the output view.

Passing the three doubles to the assembly language routine and passing the result back relies on the calling convention, which passes the three values in registers D0 to D2, and returns the result in D0. In the next article I’ll look at those calling conventions, which are so crucial to success in assembly language.


Stephen Smith (2020) Programming with 64-Bit ARM Assembly Language, Apress, ISBN 978 1 4842 5880 4.
Daniel Kusswurm (2020) Modern Arm Assembly Language Programming, Apress, ISBN 978 1 4842 6266 5.

Share RecommendKeepReplyMark as Last Read

From: FJB7/6/2021 5:45:27 PM
   of 407
The 'most complicated machine humans have built' keeps US technology a decade ahead of China
John Sexton

I’ve written about this before but yesterday the NY Times published an interesting piece on the EUV lithography machines produced by ASML and how those machines really determine who can manufacture cutting edge microchips.

As you probably know, there’s a concept called Moore’s Law which suggests that the complexity of microchips doubles every two years while the cost of the chips is cut in half. And for the most part that has held true since the first CPUs were introduced in the 1970s.

But cramming more and more transistors into the same physical space gets harder to do over time. With each successive generation of chips, the number of transistors packed into a square millimeter has to climb. Actually making that happen turns out to be massively difficult. In fact, it required expertise from different companies around the world to allow for the creation of the world’s first EUV lithography machines. The machine itself is about the size and shape of a bus and costs $150 million dollars each.

Inside are a series of mirrors which reflect ultraviolent light through an image of the chip, shrinking it down so many copies can be printed onto a single silicon wafer. ASML partnered with German optical company Zeiss to produce the high end optics for the machines. But it turns out that even the best mirrors aren’t that reflective to the ultraviolet wavelengths needed to produce the small traces on the latest chips. So the light source has to be very bright to compensate. In the end, ASML settled on a design which sprays tiny droplets of molten tin. Those droplets are then hit with a powerful laser which instantly turns them into a plasma that releases a lot of ultraviolet light.

To say it’s a complicated system is underselling it substantially. An IBM senior VP calls it the most complicated machine ever built by humans. ASML has only made about 100 of them and can only make a maximum of about 50 of them in a year. But thanks to the Trump administration, China can’t buy one.

The tool, which took decades to develop and was introduced for high-volume manufacturing in 2017, costs more than $150 million. Shipping it to customers requires 40 shipping containers, 20 trucks and three Boeing 747s.

The complex machine is widely acknowledged as necessary for making the most advanced chips, an ability with geopolitical implications. The Trump administration successfully lobbied the Dutch government to block shipments of such a machine to China in 2019, and the Biden administration has shown no signs of reversing that stance.

Manufacturers can’t produce leading-edge chips without the system, and “it is only made by the Dutch firm ASML,” said Will Hunt, a research analyst at Georgetown University’s Center for Security and Emerging Technology, which has concluded that it would take China at least a decade to build its own similar equipment. “From China’s perspective, that is a frustrating thing.”…

Since ASML introduced its commercial EUV model in 2017, customers have bought about 100 of them. Buyers include Samsung and TSMC, the biggest service producing chips designed by other companies. TSMC uses the tool to make the processors designed by Apple for its latest iPhones. Intel and IBM have said EUV is crucial to their plans.

“It’s definitely the most complicated machine humans have built,” said Darío Gil, a senior vice president at IBM.

It would probably take a decade and a trillion dollars for China to replicate the European, Japanese and American supply chain that produces the parts for the ASML EUV lithography machine. That’s a long time and by the time they got it done the free would would have moved on to something even more advanced.

But once place that does have these machines is the world’s leading chipmaker, a company called TSMC which stands for Taiwan Semiconductor Manufacturing Company Co. So if you’re wondering why China is so hot to reunite Taiwan with the mainland, one reason may be that in taking over the island they would effectively seize control of the latest ASML machines which they can’t buy and can’t produce on their own. It would be the greatest theft of advanced technology in China’s history. If China wants its technology to catch up with the rest of the world, an invasion of Taiwan is probably their best bet.

Of course invading Taiwan would probably put an end to selling new EUV machines to TSMC, but the disruption of potentially having TSMC under Chinese control would potentially set back the rest of the world’s manufacturing by several years. China might not be able to catch up completely but they could jump ahead several years and set the US back at the same time. It’s one reason the US has been looking into being less dependent on places like Taiwan for our high tech manufacturing as we move forward.

Update: This primer on EUV lithography from Zeiss notes that a single image printed by the optical system contains about a terrapixel of information. That’s equivalent to 2.4 million times the number of pixels in an HDTV.

One more mind-blowing stat. If you expanded an EUV mirror to cover the entire size of Germany the largest bump on the surface would be 100 micrometers tall. That’s how perfect these optics that produce the chips in a modern iPhone are.

Share RecommendKeepReplyMark as Last Read

From: FJB7/7/2021 5:59:45 PM
   of 407
Data Structure Visualizations

Share RecommendKeepReplyMark as Last Read
Previous 10