{"id":23635098,"url":"https://github.com/joncardasis/swiftassembly","last_synced_at":"2025-08-31T10:32:08.504Z","repository":{"id":39366293,"uuid":"76590636","full_name":"joncardasis/SwiftAssembly","owner":"joncardasis","description":"A proof of concept utilizing ARM and x86 64bit Assembly code within Swift","archived":false,"fork":false,"pushed_at":"2017-06-01T14:31:36.000Z","size":1467,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T18:08:07.982Z","etag":null,"topics":["architecture","arm","assembly","ios","physical-devices","simulator","swift","x86"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joncardasis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-15T19:46:07.000Z","updated_at":"2024-10-18T15:55:51.000Z","dependencies_parsed_at":"2022-09-20T01:33:09.588Z","dependency_job_id":null,"html_url":"https://github.com/joncardasis/SwiftAssembly","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/joncardasis/SwiftAssembly","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joncardasis%2FSwiftAssembly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joncardasis%2FSwiftAssembly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joncardasis%2FSwiftAssembly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joncardasis%2FSwiftAssembly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joncardasis","download_url":"https://codeload.github.com/joncardasis/SwiftAssembly/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joncardasis%2FSwiftAssembly/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272971376,"owners_count":25024092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-31T02:00:09.071Z","response_time":79,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["architecture","arm","assembly","ios","physical-devices","simulator","swift","x86"],"created_at":"2024-12-28T05:30:37.711Z","updated_at":"2025-08-31T10:32:08.180Z","avatar_url":"https://github.com/joncardasis.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Logo](Screenshots/Banner.png)\nA proof of concept utilizing ARM and Intel x86 64bit Assembly code within Swift.\n\n\n### Knowing your architectures\nAll iOS devices released so far are based off the ARM architecture. Since the iPhone 5S we have been running on 64-bit processor and now use ARM64.\n\nNow the iOS Simulator we have all come to love is just that. A simulator. We are simulating the iOS platform using our hardware. MacOS runs on the Intel x86 64bit architecture. Therefore that is what our simulator is utilizing.\n\n###### Well what does this mean for me?\n\nIt means we can't run the same assembly code on our physical iOS device that we run on the simulator \\*gasp\\*. If we want our Swift Assembly blend to run on both physical devices and simulators we will need to write code for both architectures. Welcome to the world of early development where nothing is cross platform.\n\n\n## Linking for proper architectures\nWe need to tell [clang](http://clang.llvm.org/) that we only want to compile our ARM code when we build for devices and x86 code when we build for the simulator. The easiest way to do this is with preprocessor macros.\n\n###### Set the macros based on the sdk.\nWe'll have to do this for each build configuration.\n![Set Preprocessor Macros](Screenshots/Set-Preprocessor-Flags.png)\n\n###### Expose macros to Swift\nNow we need to expose these macros to the Swift compiler. Note that in order to expose the macro we prepend it with *-D*.\n![Swift Macros](Screenshots/Set-Swift-Flags.png)\n\n\n\nAwesome. Let's create some assembly code. Here I'll make a procedure which adds two quad words (32-bit integers)\n\n##### Let's create a .asm file for ARM:\n```Assembly\n#if ARCH_ARM64\n\n.align 4\n.global _addTwo\n_addTwo:\n    sub sp, sp, #16\n    stp x1, x0, [sp]\n    ldp x1, x0, [sp]\n    add x0, x0, x1      \n    add sp, sp, #16\n    ret\n\n#endif\n```\n\n##### And another one for Intel x86:\n```Assembly\n#if ARCH_x86_64\n\n.globl _addTwo\n_addTwo:\n    pushq %rbp              // push the base pointer\n    movq %rsp, %rbp         // establish stack frame\n    movq %rdi, -0x8(%rbp)   // first parameter\n    movq %rsi, -0x10(%rbp)  // second parameter\n    movq -0x8(%rbp), %rsi\n    addq -0x10(%rbp), %rsi  // add the second parameter to rsi\n    movq %rsi, %rax         // move result from rsi to rax to be returned\n    popq %rbp               // restore base pointer\n    retq\n#endif\n```\n\nNotice that we check for the macros here to prevent the other trying to compile for the wrong architecture.\n\nNow we need to expose these global functions we made in assembly. Now Swift can't directly link to this so we will have to do so in C.\n\nCreate a Bridging Header and add the following line:\n\n```C\nextern int addTwo(int a, int b);\n```\n\nThis exposes our assembly addTwo procedure and now we can call it in Swift! (Note that in C/Assembly relationship we link with extern and the function will be the same name minus a leading '_')\n\n```Swift\nprint(\"Assembly Say: \\(addTwo(3,5))\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoncardasis%2Fswiftassembly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoncardasis%2Fswiftassembly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoncardasis%2Fswiftassembly/lists"}