Repository: TheAlgorithms/AArch64_Assembly Branch: main Commit: e440c077d5e7 Files: 41 Total size: 310.7 KB Directory structure: gitextract_eoup5y1n/ ├── .github/ │ └── workflows/ │ └── directory_workflow.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── DIRECTORY.md ├── LICENSE ├── README.md ├── misc/ │ ├── 2048.s │ ├── hello_world.s │ ├── josephus_problem.s │ ├── perfect_numbers.s │ ├── sha1.s │ ├── sha256.s │ └── y_combinator.s ├── sorters/ │ ├── bead_sort.s │ ├── bogo_sort.s │ ├── bubble_sort.s │ ├── circle_sort.s │ ├── cocktail_sort.s │ ├── comb_sort.s │ ├── counting_sort.s │ ├── gnome_sort.s │ ├── heap_sort.s │ ├── insertion_sort.s │ ├── jort_sort.s │ ├── merge_sort.s │ ├── pancake_sort.s │ ├── patience_sort.s │ ├── permutation_sort.s │ ├── quick_sort.s │ ├── radix_sort.s │ ├── selection_sort.s │ └── shell_sort.s └── string/ ├── append.s ├── comparison.s ├── concatenation.s ├── interpolation.s ├── length.s ├── matching.s ├── prepend.s ├── substring.s └── tokenize_string.s ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/workflows/directory_workflow.yml ================================================ name: directory_md on: [push, pull_request] jobs: MainSequence: name: DIRECTORY.md runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 # v2 is broken for git diff - uses: actions/setup-python@v2 - name: Setup Git Specs run: | git config --global user.name github-actions git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY - name: Update DIRECTORY.md shell: python run: | import os from typing import Iterator URL_BASE = "https://github.com/TheAlgorithms/AArch64_Assembly/blob/main" g_output = [] def good_filepaths(top_dir: str = ".") -> Iterator[str]: fs_exts = tuple(".asm .s".split()) for dirpath, dirnames, filenames in os.walk(top_dir): dirnames[:] = [d for d in dirnames if d[0] not in "._"] for filename in filenames: if os.path.splitext(filename)[1].lower() in fs_exts: yield os.path.join(dirpath, filename).lstrip("./") def md_prefix(i): return f"{i * ' '}*" if i else "\n##" def print_path(old_path: str, new_path: str) -> str: global g_output old_parts = old_path.split(os.sep) for i, new_part in enumerate(new_path.split(os.sep)): if i + 1 > len(old_parts) or old_parts[i] != new_part: if new_part: g_output.append(f"{md_prefix(i)} {new_part.replace('_', ' ').title()}") return new_path def build_directory_md(top_dir: str = ".") -> str: global g_output old_path = "" for filepath in sorted(good_filepaths(), key=str.lower): filepath, filename = os.path.split(filepath) if filepath != old_path: old_path = print_path(old_path, filepath) indent = (filepath.count(os.sep) + 1) if filepath else 0 url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20") filename = os.path.splitext(filename.replace("_", " ").title())[0] g_output.append(f"{md_prefix(indent)} [{filename}]({url})") return "# List of all files\n" + "\n".join(g_output) with open("DIRECTORY.md", "w") as out_file: out_file.write(build_directory_md(".") + "\n") - name: Commit DIRECTORY.md run: | git commit -m "updating DIRECTORY.md" DIRECTORY.md || true git diff DIRECTORY.md git push --force origin HEAD:$GITHUB_REF || true ================================================ FILE: CODE_OF_CONDUCT.md ================================================ # Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: * Using welcoming and inclusive language * Being respectful of differing viewpoints and experiences * Gracefully accepting constructive criticism * Focusing on what is best for the community * Showing empathy towards other community members Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery and unwelcome sexual attention or advances * Trolling, insulting/derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or electronic address, without explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at siryaka@gmail.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html [homepage]: https://www.contributor-covenant.org For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq ================================================ FILE: CONTRIBUTING.md ================================================ # Contributing When contributing to this repository, if your changes are subjective, controversial or people are likely to have polarized opinions on this matter, please first discuss the change you wish to make via issue with the owners of this repository. We welcome adding new algorithms and data structures that were mentioned in books or other reputable sources. We also welcome fixing bugs in code, clarifying documentation and adding new test cases to check existing code. Please note that we have a code of conduct, please follow it in all your interactions with the project. ================================================ FILE: DIRECTORY.md ================================================ # List of all files ## Misc * [2048](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/misc/2048.s) * [Hello World](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/misc/hello_world.s) * [Josephus Problem](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/misc/josephus_problem.s) * [Perfect Numbers](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/misc/perfect_numbers.s) * [Sha1](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/misc/sha1.s) * [Sha256](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/misc/sha256.s) * [Y Combinator](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/misc/y_combinator.s) ## Sorters * [Bead Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/bead_sort.s) * [Bogo Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/bogo_sort.s) * [Bubble Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/bubble_sort.s) * [Circle Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/circle_sort.s) * [Cocktail Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/cocktail_sort.s) * [Comb Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/comb_sort.s) * [Counting Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/counting_sort.s) * [Gnome Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/gnome_sort.s) * [Heap Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/heap_sort.s) * [Insertion Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/insertion_sort.s) * [Jort Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/jort_sort.s) * [Merge Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/merge_sort.s) * [Pancake Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/pancake_sort.s) * [Patience Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/patience_sort.s) * [Permutation Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/permutation_sort.s) * [Quick Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/quick_sort.s) * [Radix Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/radix_sort.s) * [Selection Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/selection_sort.s) * [Shell Sort](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/sorters/shell_sort.s) ## String * [Append](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/string/append.s) * [Comparison](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/string/comparison.s) * [Concatenation](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/string/concatenation.s) * [Interpolation](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/string/interpolation.s) * [Length](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/string/length.s) * [Matching](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/string/matching.s) * [Prepend](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/string/prepend.s) * [Substring](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/string/substring.s) * [Tokenize String](https://github.com/TheAlgorithms/AArch64_Assembly/blob/main/string/tokenize_string.s) ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2021 The Algorithms Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # The Algorithms - AArch64_Assembly This repository contains algorithms and data structures implemented in AArch64 Assembly for eductional purposes. ## Contribution You can contribute with pleasure to this repository. Please orient on the directory structure and overall code style of this repository. If you want to ask a question or suggest something, please open an issue. ================================================ FILE: misc/2048.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/2048 This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program 2048.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" .equ SIZE, 4 .equ TOTAL, 2048 .equ BUFFERSIZE, 80 .equ KEYSIZE, 8 .equ IOCTL, 0x1D // Linux syscall .equ SIGACTION, 0x86 // Linux syscall .equ SYSPOLL, 0x16 // Linux syscall .equ CREATPOLL, 0x14 // Linux syscall .equ CTLPOLL, 0x15 // Linux syscall .equ TCGETS, 0x5401 .equ TCSETS, 0x5402 .equ ICANON, 2 .equ ECHO, 10 .equ POLLIN, 1 .equ EPOLL_CTL_ADD, 1 .equ SIGINT, 2 // Issued if the user sends an interrupt signal (Ctrl + C) .equ SIGQUIT, 3 // Issued if the user sends a quit signal (Ctrl + D) .equ SIGTERM, 15 // Software termination signal (sent by kill by default) .equ SIGTTOU, 22 /*******************************************/ /* Structures */ /********************************************/ /* structure termios see doc linux*/ .struct 0 term_c_iflag: // input modes .struct term_c_iflag + 4 term_c_oflag: // output modes .struct term_c_oflag + 4 term_c_cflag: // control modes .struct term_c_cflag + 4 term_c_lflag: // local modes .struct term_c_lflag + 4 term_c_cc: // special characters .struct term_c_cc + 40 // see length if necessary term_fin: /* structure sigaction see doc linux */ .struct 0 sa_handler: .struct sa_handler + 8 sa_mask: .struct sa_mask + 8 sa_flags: .struct sa_flags + 8 sa_sigaction: .struct sa_sigaction + 8 sa_fin: /* structure poll see doc linux */ .struct 0 poll_event: // events mask .struct poll_event + 8 poll_fd: // events returned .struct poll_fd + 8 poll_fin: /*********************************/ /* Initialized data */ /*********************************/ .data szMessOK: .asciz "Bravo !! You win. \n" szMessNotOK: .asciz "You lost !! \n" szMessNewGame: .asciz "New game (y/n) ? \n" szMessErreur: .asciz "Error detected.\n" szMessErrInitTerm: .asciz "Error terminal init.\n" szMessErrInitPoll: .asciz "Error poll init.\n" szMessErreurKey: .asciz "Error read key.\n" szMessErr: .asciz "Error code hexa : @ décimal : @ \n" szCarriageReturn: .asciz "\n" szMess0: .asciz " " szMess2: .asciz " 2 " szMess4: .asciz " 4 " szMess8: .asciz " 8 " szMess16: .asciz " 16 " szMess32: .asciz " 32 " szMess64: .asciz " 64 " szMess128: .asciz " 128 " szMess256: .asciz " 256 " szMess512: .asciz " 512 " szMess1024: .asciz " 1024 " szMess2048: .asciz " 2048 " szCleax1: .byte 0x1B .byte 'c' // other console clear .byte 0 szLineH: .asciz "-----------------------------\n" szLineV: .asciz "|" szLineVT: .asciz "| | | | |\n" .align 4 qGraine: .quad 123456 /*********************************/ /* UnInitialized data */ /*********************************/ .bss .align 4 sZoneConv: .skip 24 sBuffer: .skip BUFFERSIZE qTbCase: .skip 8 * SIZE * SIZE qEnd: .skip 8 // 0 loop 1 = end loop qTouche: .skip KEYSIZE // value key pressed stOldtio: .skip term_fin // old terminal state stCurtio: .skip term_fin // current terminal state stSigAction: .skip sa_fin // area signal structure stSigAction1: .skip sa_fin stSigAction2: .skip sa_fin stSigAction3: .skip sa_fin stPoll1: .skip poll_fin // area poll structure stPoll2: .skip poll_fin stevents: .skip 16 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program mov x0,#0 bl initTerm // terminal init cmp x0,0 // error ? blt 100f bl initPoll // epoll instance init cmp x0,0 blt 99f mov x22,x0 // save epfd 1: // begin game loop ldr x0,qAdrszCleax1 bl affichageMess bl razTable 2: bl addDigit cmp x0,#-1 beq 5f // end game bl displayGame 3: mov x0,x22 bl waitKey cmp x0,0 beq 3b bl readKey cmp x0,#-1 beq 99f // error bl keyMove cmp x0,#0 beq 3b // no change -> loop cmp x0,#2 // last addition = 2048 ? beq 4f cmp x0,#-1 // quit ? bne 2b // loop b 10f 4: // last addition = 2048 ldr x0,qAdrszMessOK bl affichageMess b 10f 5: // display message no solution ldr x0,qAdrszMessNotOK bl affichageMess 10: // display new game ? ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszMessNewGame bl affichageMess 11: mov x0,x22 bl waitKey cmp x0,0 beq 11b bl readKey ldr x0,qAdrqTouche ldrb w0,[x0] cmp w0,#'y' beq 1b cmp w0,#'Y' beq 1b 99: bl restauTerm // terminal restaur 100: // standard end of the program mov x0, #0 // return code mov x8, #EXIT // request to exit program svc #0 // perform the system call qAdrszCarriageReturn: .quad szCarriageReturn qAdrszMessNotOK: .quad szMessNotOK qAdrszMessOK: .quad szMessOK qAdrszMessNewGame: .quad szMessNewGame qAdrsZoneConv: .quad sZoneConv qAdrszCleax1: .quad szCleax1 qAdrszMessErrInitTerm: .quad szMessErrInitTerm qAdrszMessErrInitPoll: .quad szMessErrInitPoll qAdrszMessErreurKey: .quad szMessErreurKey qAdrstOldtio: .quad stOldtio qAdrstCurtio: .quad stCurtio qAdrstSigAction: .quad stSigAction qAdrstSigAction1: .quad stSigAction1 qAdrSIG_IGN: .quad 1 qAdrqEnd: .quad qEnd qAdrqTouche: .quad qTouche qAdrstevents: .quad stevents /******************************************************************/ /* raz table cases */ /******************************************************************/ razTable: stp x0,lr,[sp,-16]! // save registres stp x1,x2,[sp,-16]! // save registres ldr x1,qAdrqTbCase mov x2,#0 1: str xzr,[x1,x2,lsl #3] add x2,x2,#1 cmp x2,#SIZE * SIZE blt 1b 100: ldp x1,x2,[sp],16 // restaur des 2 registres ldp x0,lr,[sp],16 // restaur des 2 registres ret /******************************************************************/ /* key move */ /******************************************************************/ /* x0 contains key value */ keyMove: stp x1,lr,[sp,-16]! // save registres lsr x0,x0,#16 cmp x0,#0x42 // down arrow bne 1f bl moveDown b 100f 1: cmp x0,#0x41 // high arrow bne 2f bl moveUp b 100f 2: cmp x0,#0x43 // right arrow bne 3f bl moveRight b 100f 3: cmp x0,#0x44 // left arrow bne 4f bl moveLeft b 100f 4: ldr x0,qAdrqTouche ldrb w0,[x0] cmp w0,#'q' // quit game bne 5f mov x0,#-1 b 100f 5: cmp w0,#'Q' // quit game bne 100f mov x0,#-1 b 100f 100: ldp x1,lr,[sp],16 // restaur des 2 registres ret /******************************************************************/ /* move left */ /******************************************************************/ /* x0 return -1 if ok */ moveLeft: stp x1,lr,[sp,-16]! // save registres stp x2,x3,[sp,-16]! // save registres stp x4,x5,[sp,-16]! // save registres stp x6,x7,[sp,-16]! // save registres stp x8,x9,[sp,-16]! // save registres stp x10,x11,[sp,-16]! // save registres ldr x1,qAdrqTbCase mov x0,#0 // top move Ok mov x2,#0 // line indice 1: mov x6,#0 // counter empty case mov x7,#0 // first digit mov x10,#0 // last digit to add mov x3,#0 // column indice 2: lsl x5,x2,#2 // change this if size <> 4 add x5,x5,x3 // compute table indice ldr x4,[x1,x5,lsl #3] cmp x4,#0 cinc x6,x6,eq // positions vides beq 5f cmp x6,#0 beq 3f // no empty left case mov x8,#0 str x8,[x1,x5,lsl #3] // raz digit sub x5,x5,x6 str x4,[x1,x5,lsl #3] // and store to left empty position mov x0,#1 // move Ok 3: cmp x7,#0 // first digit beq 4f cmp x10,x4 // prec digit have to add beq 4f sub x8,x5,#1 // prec digit ldr x9,[x1,x8,lsl #3] cmp x4,x9 // equal ? bne 4f mov x10,x4 // save digit add x4,x4,x9 // yes -> add str x4,[x1,x8,lsl #3] cmp x4,#TOTAL beq 6f mov x4,#0 str x4,[x1,x5,lsl #3] add x6,x6,#1 // empty case + 1 mov x0,#1 // move Ok 4: add x7,x7,#1 // no first digit 5: // and loop add x3,x3,#1 cmp x3,#SIZE blt 2b add x2,x2,#1 cmp x2,#SIZE blt 1b b 100f 6: mov x0,#2 // total = 2048 100: ldp x10,x11,[sp],16 // restaur des 2 registres ldp x8,x9,[sp],16 // restaur des 2 registres ldp x6,x7,[sp],16 // restaur des 2 registres ldp x4,x5,[sp],16 // restaur des 2 registres ldp x2,x3,[sp],16 // restaur des 2 registres ldp x1,lr,[sp],16 // restaur des 2 registres ret /******************************************************************/ /* move right */ /******************************************************************/ /* x0 return -1 if ok */ moveRight: stp x1,lr,[sp,-16]! // save registres stp x2,x3,[sp,-16]! // save registres stp x4,x5,[sp,-16]! // save registres stp x6,x7,[sp,-16]! // save registres stp x8,x9,[sp,-16]! // save registres stp x10,x11,[sp,-16]! // save registres ldr x1,qAdrqTbCase mov x0,#0 mov x2,#0 1: mov x6,#0 mov x7,#0 mov x10,#0 mov x3,#SIZE-1 2: lsl x5,x2,#2 // change this if size <> 4 add x5,x5,x3 ldr x4,[x1,x5,lsl #3] cmp x4,#0 cinc x6,x6,eq // positions vides beq 5f cmp x6,#0 beq 3f // no empty right case mov x0,#0 str x0,[x1,x5,lsl #3] // raz digit add x5,x5,x6 str x4,[x1,x5,lsl #3] // and store to right empty position mov x0,#1 3: cmp x7,#0 // first digit beq 4f add x8,x5,#1 // next digit ldr x9,[x1,x8,lsl #3] cmp x4,x9 // equal ? bne 4f cmp x10,x4 beq 4f mov x10,x4 add x4,x4,x9 // yes -> add str x4,[x1,x8,lsl #3] cmp x4,#TOTAL beq 6f mov x4,#0 str x4,[x1,x5,lsl #3] add x6,x6,#1 // empty case + 1 mov x0,#1 4: add x7,x7,#1 // no first digit 5: // and loop sub x3,x3,#1 cmp x3,#0 bge 2b add x2,x2,#1 cmp x2,#SIZE blt 1b b 100f 6: mov x0,#2 100: ldp x10,x11,[sp],16 // restaur des 2 registres ldp x8,x9,[sp],16 // restaur des 2 registres ldp x6,x7,[sp],16 // restaur des 2 registres ldp x4,x5,[sp],16 // restaur des 2 registres ldp x2,x3,[sp],16 // restaur des 2 registres ldp x1,lr,[sp],16 // restaur des 2 registres ret /******************************************************************/ /* move down */ /******************************************************************/ /* x0 return -1 if ok */ moveDown: stp x1,lr,[sp,-16]! // save registres stp x2,x3,[sp,-16]! // save registres stp x4,x5,[sp,-16]! // save registres stp x6,x7,[sp,-16]! // save registres stp x8,x9,[sp,-16]! // save registres stp x10,x11,[sp,-16]! // save registres ldr x1,qAdrqTbCase mov x0,#0 mov x3,#0 1: mov x6,#0 mov x7,#0 mov x10,#0 mov x2,#SIZE-1 2: lsl x5,x2,#2 // change this if size <> 4 add x5,x5,x3 ldr x4,[x1,x5,lsl #3] cmp x4,#0 cinc x6,x6,eq // positions vides beq 5f cmp x6,#0 beq 3f // no empty right case mov x0,#0 str x0,[x1,x5,lsl #3] // raz digit lsl x0,x6,#2 add x5,x5,x0 str x4,[x1,x5,lsl #3] // and store to right empty position mov x0,#1 3: cmp x7,#0 // first digit beq 4f add x8,x5,#SIZE // down digit ldr x9,[x1,x8,lsl #3] cmp x4,x9 // equal ? bne 4f cmp x10,x4 beq 4f mov x10,x4 add x4,x4,x9 // yes -> add str x4,[x1,x8,lsl #3] cmp x4,#TOTAL beq 6f mov x4,#0 str x4,[x1,x5,lsl #3] add x6,x6,#1 // empty case + 1 mov x0,#1 4: add x7,x7,#1 // no first digit 5: // and loop sub x2,x2,#1 cmp x2,#0 bge 2b add x3,x3,#1 cmp x3,#SIZE blt 1b b 100f 6: mov x0,#2 100: ldp x10,x11,[sp],16 // restaur des 2 registres ldp x8,x9,[sp],16 // restaur des 2 registres ldp x6,x7,[sp],16 // restaur des 2 registres ldp x4,x5,[sp],16 // restaur des 2 registres ldp x2,x3,[sp],16 // restaur des 2 registres ldp x1,lr,[sp],16 // restaur des 2 registres ret /******************************************************************/ /* move up */ /******************************************************************/ /* x0 return -1 if ok */ moveUp: stp x1,lr,[sp,-16]! // save registres stp x2,x3,[sp,-16]! // save registres stp x4,x5,[sp,-16]! // save registres stp x6,x7,[sp,-16]! // save registres stp x8,x9,[sp,-16]! // save registres stp x10,x11,[sp,-16]! // save registres ldr x1,qAdrqTbCase mov x0,#0 mov x3,#0 1: mov x6,#0 mov x7,#0 mov x10,#0 mov x2,#0 2: lsl x5,x2,#2 // change this if size <> 4 add x5,x5,x3 ldr x4,[x1,x5,lsl #3] cmp x4,#0 cinc x6,x6,eq // positions vides beq 5f cmp x6,#0 beq 3f // no empty right case mov x0,#0 str x0,[x1,x5,lsl #3] // raz digit lsl x0,x6,#2 sub x5,x5,x0 str x4,[x1,x5,lsl #3] // and store to right empty position mov x0,#1 3: cmp x7,#0 // first digit beq 4f sub x8,x5,#SIZE // up digit ldr x9,[x1,x8,lsl #3] cmp x4,x9 // equal ? bne 4f cmp x10,x4 beq 4f mov x10,x4 add x4,x4,x9 // yes -> add str x4,[x1,x8,lsl #3] cmp x4,#TOTAL beq 6f mov x4,#0 str x4,[x1,x5,lsl #3] add x6,x6,#1 // empty case + 1 mov x0,#1 4: add x7,x7,#1 // no first digit 5: // and loop add x2,x2,#1 cmp x2,#SIZE blt 2b add x3,x3,#1 cmp x3,#SIZE blt 1b b 100f 6: mov x0,#2 100: ldp x10,x11,[sp],16 // restaur des 2 registres ldp x8,x9,[sp],16 // restaur des 2 registres ldp x6,x7,[sp],16 // restaur des 2 registres ldp x4,x5,[sp],16 // restaur des 2 registres ldp x2,x3,[sp],16 // restaur des 2 registres ldp x1,lr,[sp],16 // restaur des 2 registres ret /******************************************************************/ /* add new digit on game */ /******************************************************************/ /* x0 return -1 if ok */ addDigit: stp x1,lr,[sp,-16]! // save registres stp x2,x3,[sp,-16]! // save registres stp x4,x5,[sp,-16]! // save registres sub sp,sp,#8 * SIZE*SIZE mov fp,sp mov x0,#100 bl genereraleas cmp x0,#10 mov x1,#4 mov x5,#2 csel x5,x5,x1,ge // movlt x5,#4 //movge x5,#2 ldr x1,qAdrqTbCase mov x3,#0 mov x4,#0 1: ldr x2,[x1,x3,lsl 3] cmp x2,#0 bne 2f str x3,[fp,x4,lsl 3] add x4,x4,#1 2: add x3,x3,#1 cmp x3,#SIZE*SIZE blt 1b cmp x4,#0 // no empty case beq 4f cmp x4,#1 bne 3f ldr x2,[fp] // one case str x5,[x1,x2,lsl 3] mov x0,#0 b 100f 3: // multiple case sub x0,x4,#1 bl genereraleas ldr x2,[fp,x0,lsl 3] str x5,[x1,x2,lsl 3] mov x0,#0 b 100f 4: mov x0,#-1 100: add sp,sp,#8* (SIZE*SIZE) // stack alignement ldp x4,x5,[sp],16 // restaur des 2 registres ldp x2,x3,[sp],16 // restaur des 2 registres ldp x1,lr,[sp],16 // restaur des 2 registres ret qAdrqTbCase: .quad qTbCase /******************************************************************/ /* display game */ /******************************************************************/ displayGame: stp x1,lr,[sp,-16]! // save registres stp x2,x3,[sp,-16]! // save registres ldr x0,qAdrszCleax1 bl affichageMess ldr x0,qAdrszLineH bl affichageMess ldr x0,qAdrszLineVT bl affichageMess ldr x0,qAdrszLineV bl affichageMess ldr x1,qAdrqTbCase mov x2,#0 1: ldr x0,[x1,x2,lsl #3] bl digitString bl affichageMess ldr x0,qAdrszLineV bl affichageMess add x2,x2,#1 cmp x2,#SIZE blt 1b ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszLineVT bl affichageMess ldr x0,qAdrszLineH bl affichageMess ldr x0,qAdrszLineVT bl affichageMess ldr x0,qAdrszLineV bl affichageMess 2: ldr x0,[x1,x2,lsl #3] bl digitString bl affichageMess ldr x0,qAdrszLineV bl affichageMess add x2,x2,#1 cmp x2,#SIZE*2 blt 2b ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszLineVT bl affichageMess ldr x0,qAdrszLineH bl affichageMess ldr x0,qAdrszLineVT bl affichageMess ldr x0,qAdrszLineV bl affichageMess 3: ldr x0,[x1,x2,lsl #3] bl digitString bl affichageMess ldr x0,qAdrszLineV bl affichageMess add x2,x2,#1 cmp x2,#SIZE*3 blt 3b ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszLineVT bl affichageMess ldr x0,qAdrszLineH bl affichageMess ldr x0,qAdrszLineVT bl affichageMess ldr x0,qAdrszLineV bl affichageMess 4: ldr x0,[x1,x2,lsl #3] bl digitString bl affichageMess ldr x0,qAdrszLineV bl affichageMess add x2,x2,#1 cmp x2,#SIZE*4 blt 4b ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszLineVT bl affichageMess ldr x0,qAdrszLineH bl affichageMess 100: ldp x2,x3,[sp],16 // restaur des 2 registres ldp x1,lr,[sp],16 // restaur des 2 registres ret qAdrszLineH: .quad szLineH qAdrszLineV: .quad szLineV qAdrszLineVT: .quad szLineVT /******************************************************************/ /* digits string */ /******************************************************************/ /* x0 contains number */ /* x0 return address string */ digitString: stp x1,lr,[sp,-16]! // save registres cmp x0,#0 bne 1f ldr x0,qAdrszMess0 b 100f 1: cmp x0,#2 bne 2f ldr x0,qAdrszMess2 b 100f 2: cmp x0,#4 bne 3f ldr x0,qAdrszMess4 b 100f 3: cmp x0,#8 bne 4f ldr x0,qAdrszMess8 b 100f 4: cmp x0,#16 bne 5f ldr x0,qAdrszMess16 b 100f 5: cmp x0,#32 bne 6f ldr x0,qAdrszMess32 b 100f 6: cmp x0,#64 bne 7f ldr x0,qAdrszMess64 b 100f 7: cmp x0,#128 bne 8f ldr x0,qAdrszMess128 b 100f 8: cmp x0,#256 bne 9f ldr x0,qAdrszMess256 b 100f 9: cmp x0,#512 bne 10f ldr x0,qAdrszMess512 b 100f 10: cmp x0,#1024 bne 11f ldr x0,qAdrszMess1024 b 100f 11: cmp x0,#2048 bne 12f ldr x0,qAdrszMess2048 b 100f 12: ldr x1,qAdrszMessErreur // error message bl displayError 100: ldp x1,lr,[sp],16 // restaur des 2 registres ret qAdrszMess0: .quad szMess0 qAdrszMess2: .quad szMess2 qAdrszMess4: .quad szMess4 qAdrszMess8: .quad szMess8 qAdrszMess16: .quad szMess16 qAdrszMess32: .quad szMess32 qAdrszMess64: .quad szMess64 qAdrszMess128: .quad szMess128 qAdrszMess256: .quad szMess256 qAdrszMess512: .quad szMess512 qAdrszMess1024: .quad szMess1024 qAdrszMess2048: .quad szMess2048 //qAdrsBuffer: .quad sBuffer qAdrszMessErreur : .quad szMessErreur /***************************************************/ /* Generation random number */ /***************************************************/ /* x0 contains limit */ genereraleas: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers ldr x1,qAdrqGraine ldr x2,[x1] ldr x3,qNbDep1 mul x2,x3,x2 ldr x3,qNbDep2 add x2,x2,x3 str x2,[x1] // maj de la graine pour l appel suivant cmp x0,#0 beq 100f udiv x3,x2,x0 msub x0,x3,x0,x2 // résult = remainder 100: // end function ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /*****************************************************/ qAdrqGraine: .quad qGraine qNbDep1: .quad 0x0019660d qNbDep2: .quad 0x3c6ef35f /******************************************************************/ /* traitement du signal */ /******************************************************************/ sighandler: stp x0,lr,[sp,-16]! // save registers str x1,[sp,-16]! ldr x0,qAdrqEnd mov x1,#1 // maj zone end str x1,[x0] ldr x1,[sp],16 ldp x0,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /***************************************************/ /* display error message */ /***************************************************/ /* x0 contains error code x1 : message address */ displayError: stp x2,lr,[sp,-16]! // save registers mov x2,x0 // save error code mov x0,x1 bl affichageMess mov x0,x2 // error code ldr x1,qAdrsZoneConv bl conversion16 // conversion hexa ldr x0,qAdrszMessErr // display error message ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character mov x3,x0 mov x0,x2 // error code ldr x1,qAdrsZoneConv // result address bl conversion10S // conversion decimale mov x0,x3 ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess 100: ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrszMessErr: .quad szMessErr /*********************************/ /* init terminal state */ /*********************************/ initTerm: stp x1,lr,[sp,-16]! // save registers /* read terminal state */ mov x0,STDIN // input console mov x1,TCGETS ldr x2,qAdrstOldtio mov x8,IOCTL // call system Linux svc 0 cbnz x0,98f // error ? adr x0,sighandler // adresse routine traitement signal ldr x1,qAdrstSigAction // adresse structure sigaction str x0,[x1,sa_handler] // maj handler mov x0,SIGINT // signal type ldr x1,qAdrstSigAction mov x2,0 mov x3,8 mov x8,SIGACTION // call system svc 0 cmp x0,0 // error ? bne 98f mov x0,SIGQUIT ldr x1,qAdrstSigAction mov x2,0 // NULL mov x8,SIGACTION // call system svc 0 cmp x0,0 // error ? bne 98f mov x0,SIGTERM ldr x1,qAdrstSigAction mov x2,0 // NULL mov x8,SIGACTION // appel systeme svc 0 cmp x0,0 bne 98f // adr x0,qAdrSIG_IGN // address signal igonre function ldr x1,qAdrstSigAction1 str x0,[x1,sa_handler] mov x0,SIGTTOU //invalidate other process signal ldr x1,qAdrstSigAction1 mov x2,0 // NULL mov x8,SIGACTION // call system svc 0 cmp x0,0 bne 98f // /* read terminal current state */ mov x0,STDIN mov x1,TCGETS ldr x2,qAdrstCurtio // address current termio mov x8,IOCTL // call systeme svc 0 cmp x0,0 // error ? bne 98f mov x2,ICANON | ECHO // no key pressed echo on display mvn x2,x2 // and one key ldr x1,qAdrstCurtio ldr x3,[x1,#term_c_lflag] and x3,x2,x2 // add flags str x3,[x1,#term_c_lflag] // and store mov x0,STDIN // maj terminal current state mov x1,TCSETS ldr x2,qAdrstCurtio mov x8,IOCTL // call system svc 0 cbz x0,100f 98: // error display ldr x1,qAdrszMessErrInitTerm bl displayError mov x0,-1 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrstSigAction2: .quad stSigAction2 qAdrstSigAction3: .quad stSigAction3 /*********************************/ /* init instance epool */ /*********************************/ initPoll: stp x1,lr,[sp,-16]! // save registers ldr x0,qAdrstevents mov x1,STDIN // maj structure events str x1,[x0,#poll_fd] // maj FD mov x1,POLLIN // action code str x1,[x0,#poll_event] mov x0,0 mov x8,CREATPOLL // create epoll instance svc 0 cmp x0,0 // error ? ble 98f mov x10,x0 // return FD epoll instance mov x1,EPOLL_CTL_ADD mov x2,STDIN // Fd to we want add ldr x3,qAdrstevents // structure events address mov x8,CTLPOLL // call system control epoll svc 0 cmp x0,0 // error ? blt 98f // no mov x0,x10 // return FD epoll instance b 100f 98: // error display ldr x1,qAdrszMessErrInitPoll // error message bl displayError mov x0,-1 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /*********************************/ /* wait key */ /*********************************/ /* x0 contains FD poll */ waitKey: stp x1,lr,[sp,-16]! // save registers ldr x11,qAdrqTouche // key address str xzr,[x11] // raz key 1: ldr x1,qAdrqEnd // if signal ctrl-c -> end ldr x1,[x1] cbnz x1,100f ldr x1,qAdrstevents mov x2,12 // size events mov x3,1 // timeout = 1 TODO: ?? mov x4,0 mov x8,SYSPOLL // call system wait POLL svc 0 cmp x0,0 // key pressed ? bge 100f 98: // error display ldr x1,qAdrszMessErreurKey // error message bl displayError mov x0,-1 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /*********************************/ /* read key */ /*********************************/ /* x0 returns key value */ readKey: stp x1,lr,[sp,-16]! // save registers mov x0,STDIN // File Descriptor ldr x1,qAdrqTouche // buffer address mov x2,KEYSIZE // key size mov x8,READ // read key svc #0 cmp x0,0 // error ? ble 98f ldr x2,qAdrqTouche // key address ldr x0,[x2] b 100f 98: // error display ldr x1,qAdrszMessErreur // error message bl displayError mov x0,-1 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /*********************************/ /* restaur terminal state */ /*********************************/ restauTerm: stp x1,lr,[sp,-16]! // save registers mov x0,STDIN // end then restaur begin state terminal mov x1,TCSETS ldr x2,qAdrstOldtio mov x8,IOCTL // call system svc 0 cbz x0,100f ldr x1,qAdrszMessErreur // error message bl displayError 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: misc/hello_world.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Hello_world/Text This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ .equ STDOUT, 1 .equ SVC_WRITE, 64 .equ SVC_EXIT, 93 .text .global _start _start: stp x29, x30, [sp, -16]! mov x0, #STDOUT ldr x1, =msg mov x2, 13 mov x8, #SVC_WRITE mov x29, sp svc #0 // write(stdout, msg, 13); ldp x29, x30, [sp], 16 mov x0, #0 mov x8, #SVC_EXIT svc #0 // exit(0); msg: .ascii "Hello World!\n" .align 4 ================================================ FILE: misc/josephus_problem.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Josephus_problem This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program josephus_problem.s */ /* run with josephus_problem maxi intervalle */ /* example : josephus_problem 41 3 /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" .equ FIRSTNODE, 0 //identification first node /*******************************************/ /* Structures */ /********************************************/ /* structure linkedlist*/ .struct 0 llist_next: // next element .struct llist_next + 8 llist_value: // element value .struct llist_value + 8 llist_fin: /*********************************/ /* Initialized data */ /*********************************/ .data szMessDebutPgm: .asciz "Start program.\n" szMessFinPgm: .asciz "Program End ok.\n" szRetourLigne: .asciz "\n" szMessValElement: .asciz "Value : @ \n" szMessListeVide: .asciz "List empty.\n" szMessImpElement: .asciz "Node display: @ Value : @ Next @ \n" szMessErrComm: .asciz "Incomplete Command line : josephus64 \n" /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 100 .align 4 qDebutListe1: .skip llist_fin /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program mov fp,sp // copy stack address register x29 fp ldr x0,qAdrszMessDebutPgm bl affichageMess ldr x0,[fp] // parameter number command line cmp x0,#2 // correct ? ble erreurCommande // error add x0,fp,#16 // address parameter 2 ldr x0,[x0] bl conversionAtoD add x22,x0,FIRSTNODE // save maxi add x0,fp,#24 // address parameter 3 ldr x0,[x0] bl conversionAtoD mov x21,x0 // save gap mov x0,FIRSTNODE // create first node mov x1,0 bl createNode mov x25,x0 // first node address mov x26,x0 mov x24,FIRSTNODE + 1 mov x23,1 1: // loop create others nodes mov x0,x24 // key value mov x1,0 bl createNode str x0,[x26,llist_next] // store current node address in prev node mov x26,x0 add x24,x24,1 add x23,x23,1 cmp x23,x22 // maxi ? blt 1b str x25,[x26,llist_next] // store first node address in last pointer mov x24,x26 2: mov x20,1 // counter for gap 3: ldr x24,[x24,llist_next] add x20,x20,1 cmp x20,x21 // intervalle ? blt 3b ldr x25,[x24,llist_next] // removing the node from the list ldr x22,[x25,llist_value] ldr x27,[x25,llist_next] // load pointer next str x27,[x24,llist_next] // ans store in prev node //mov x0,x25 //bl displayNode cmp x27,x24 csel x24,x24,x27,ne // next node address bne 2b // and loop mov x0,x24 bl displayNode // display last node b 100f erreurCommande: ldr x0,qAdrszMessErrComm bl affichageMess mov x0,#1 // error code b 100f 100: // program end standard ldr x0,qAdrszMessFinPgm bl affichageMess mov x0,0 // return code Ok mov x8,EXIT // system call "Exit" svc #0 qAdrszMessDebutPgm: .quad szMessDebutPgm qAdrszMessFinPgm: .quad szMessFinPgm qAdrszRetourLigne: .quad szRetourLigne qAdrqDebutListe1: .quad qDebutListe1 qAdrszMessErrComm: .quad szMessErrComm /******************************************************************/ /* create node */ /******************************************************************/ /* x0 contains key */ /* x1 contains zero or address next node */ /* x0 returns address heap node */ createNode: stp x20,lr,[sp,-16]! // save registres stp x21,x22,[sp,-16]! // save registres mov x20,x0 // save key mov x21,x1 // save key mov x0,#0 // allocation place heap mov x8,BRK // call system 'brk' svc #0 mov x22,x0 // save address heap for node add x0,x0,llist_fin // reservation place node length mov x8,BRK // call system 'brk' svc #0 cmp x0,#-1 // allocation error beq 100f str x20,[x22,llist_value] str x21,[x22,llist_next] mov x0,x22 100: ldp x21,x22,[sp],16 // restaur des 2 registres ldp x20,lr,[sp],16 // restaur des 2 registres ret // retour adresse lr x30 /******************************************************************/ /* display infos node */ /******************************************************************/ /* x0 contains node address */ displayNode: stp x1,lr,[sp,-16]! // save registres stp x2,x3,[sp,-16]! // save registres mov x2,x0 ldr x1,qAdrsZoneConv bl conversion16 ldr x0,qAdrszMessImpElement ldr x1,qAdrsZoneConv bl strInsertAtCharInc mov x3,x0 ldr x0,[x2,llist_value] ldr x1,qAdrsZoneConv bl conversion10S mov x0,x3 ldr x1,qAdrsZoneConv bl strInsertAtCharInc mov x3,x0 ldr x0,[x2,llist_next] ldr x1,qAdrsZoneConv bl conversion16 mov x0,x3 ldr x1,qAdrsZoneConv bl strInsertAtCharInc bl affichageMess 100: ldp x2,x3,[sp],16 // restaur des 2 registres ldp x1,lr,[sp],16 // restaur des 2 registres ret // retour adresse lr x30 qAdrsZoneConv: .quad sZoneConv qAdrszMessImpElement: .quad szMessImpElement /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: misc/perfect_numbers.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Perfect_numbers This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program perfect_numbers.s */ /* use Euclide Formula : if M=(2puis p)-1 is prime M * (M+1)/2 is perfect see Wikipedia */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" .equ MAXI, 63 /*********************************/ /* Initialized data */ /*********************************/ .data sMessResult: .asciz "Perfect : @ \n" szMessOverflow: .asciz "Overflow in function isPrime.\n" szCarriageReturn: .asciz "\n" /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program mov x4,2 // start 2 mov x3,1 // counter 2 power 1: // begin loop lsl x4,x4,1 // 2 power sub x0,x4,1 // - 1 bl isPrime // is prime ? cbz x0,2f // no sub x0,x4,1 // yes mul x1,x0,x4 // multiply m by m-1 lsr x0,x1,1 // divide by 2 bl displayPerfect // and display 2: add x3,x3,1 // next power of 2 cmp x3,MAXI blt 1b 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult /******************************************************************/ /* Display perfect number */ /******************************************************************/ /* x0 contains the number */ displayPerfect: stp x1,lr,[sp,-16]! // save registers ldr x1,qAdrsZoneConv bl conversion10 // call décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv // insert conversion in message bl strInsertAtCharInc bl affichageMess // display message 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrsZoneConv: .quad sZoneConv /***************************************************/ /* is a number prime ? */ /***************************************************/ /* x0 contains the number */ /* x0 return 1 if prime else 0 */ //2147483647 OK //4294967297 NOK //131071 OK //1000003 OK //10001363 OK isPrime: stp x1,lr,[sp,-16]! // save registres stp x2,x3,[sp,-16]! // save registres mov x2,x0 sub x1,x0,#1 cmp x2,0 beq 99f // return zero cmp x2,2 // for 1 and 2 return 1 ble 2f mov x0,#2 bl moduloPuR64 bcs 100f // error overflow cmp x0,#1 bne 99f // no prime cmp x2,3 beq 2f mov x0,#3 bl moduloPuR64 blt 100f // error overflow cmp x0,#1 bne 99f cmp x2,5 beq 2f mov x0,#5 bl moduloPuR64 bcs 100f // error overflow cmp x0,#1 bne 99f // Pas premier cmp x2,7 beq 2f mov x0,#7 bl moduloPuR64 bcs 100f // error overflow cmp x0,#1 bne 99f // Pas premier cmp x2,11 beq 2f mov x0,#11 bl moduloPuR64 bcs 100f // error overflow cmp x0,#1 bne 99f // Pas premier cmp x2,13 beq 2f mov x0,#13 bl moduloPuR64 bcs 100f // error overflow cmp x0,#1 bne 99f // Pas premier 2: cmn x0,0 // carry à zero no error mov x0,1 // prime b 100f 99: cmn x0,0 // carry à zero no error mov x0,#0 // prime 100: ldp x2,x3,[sp],16 // restaur des 2 registres ldp x1,lr,[sp],16 // restaur des 2 registres ret /**************************************************************/ /********************************************************/ /* Compute modulo de b power e modulo m */ /* Exemple 4 puissance 13 modulo 497 = 445 */ /********************************************************/ /* x0 number */ /* x1 exposant */ /* x2 modulo */ moduloPuR64: stp x1,lr,[sp,-16]! // save registres stp x3,x4,[sp,-16]! // save registres stp x5,x6,[sp,-16]! // save registres stp x7,x8,[sp,-16]! // save registres stp x9,x10,[sp,-16]! // save registres cbz x0,100f cbz x1,100f mov x8,x0 mov x7,x1 mov x6,1 // result udiv x4,x8,x2 msub x9,x4,x2,x8 // remainder 1: tst x7,1 // if bit = 1 beq 2f mul x4,x9,x6 umulh x5,x9,x6 mov x6,x4 mov x0,x6 mov x1,x5 bl divisionReg128U // division 128 bits cbnz x1,99f // overflow mov x6,x3 // remainder 2: mul x8,x9,x9 umulh x5,x9,x9 mov x0,x8 mov x1,x5 bl divisionReg128U cbnz x1,99f // overflow mov x9,x3 lsr x7,x7,1 cbnz x7,1b mov x0,x6 // result cmn x0,0 // carry à zero no error b 100f 99: ldr x0,qAdrszMessOverflow bl affichageMess // display error message cmp x0,0 // carry set error mov x0,-1 // code erreur 100: ldp x9,x10,[sp],16 // restaur des 2 registres ldp x7,x8,[sp],16 // restaur des 2 registres ldp x5,x6,[sp],16 // restaur des 2 registres ldp x3,x4,[sp],16 // restaur des 2 registres ldp x1,lr,[sp],16 // restaur des 2 registres ret // retour adresse lr x30 qAdrszMessOverflow: .quad szMessOverflow /***************************************************/ /* division d un nombre de 128 bits par un nombre de 64 bits */ /***************************************************/ /* x0 contient partie basse dividende */ /* x1 contient partie haute dividente */ /* x2 contient le diviseur */ /* x0 retourne partie basse quotient */ /* x1 retourne partie haute quotient */ /* x3 retourne le reste */ divisionReg128U: stp x6,lr,[sp,-16]! // save registres stp x4,x5,[sp,-16]! // save registres mov x5,#0 // raz du reste R mov x3,#128 // compteur de boucle mov x4,#0 // dernier bit 1: lsl x5,x5,#1 // on decale le reste de 1 tst x1,1<<63 // test du bit le plus à gauche lsl x1,x1,#1 // on decale la partie haute du quotient de 1 beq 2f orr x5,x5,#1 // et on le pousse dans le reste R 2: tst x0,1<<63 lsl x0,x0,#1 // puis on decale la partie basse beq 3f orr x1,x1,#1 // et on pousse le bit de gauche dans la partie haute 3: orr x0,x0,x4 // position du dernier bit du quotient mov x4,#0 // raz du bit cmp x5,x2 blt 4f sub x5,x5,x2 // on enleve le diviseur du reste mov x4,#1 // dernier bit à 1 4: // et boucle subs x3,x3,#1 bgt 1b lsl x1,x1,#1 // on decale le quotient de 1 tst x0,1<<63 lsl x0,x0,#1 // puis on decale la partie basse beq 5f orr x1,x1,#1 5: orr x0,x0,x4 // position du dernier bit du quotient mov x3,x5 100: ldp x4,x5,[sp],16 // restaur des 2 registres ldp x6,lr,[sp],16 // restaur des 2 registres ret // retour adresse lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: misc/sha1.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/SHA-1 This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program sha1.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" .equ SHA_DIGEST_LENGTH, 20 //.include "../../ficmacros64.s" /*********************************/ /* Initialized data */ /*********************************/ .data szMessRosetta: .asciz "Rosetta Code" szMessTest1: .asciz "abc" szMessSup64: .ascii "ABCDEFGHIJKLMNOPQRSTUVWXYZ" .ascii "abcdefghijklmnopqrstuvwxyz" .asciz "1234567890AZERTYUIOP" szMessTest2: .asciz "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" szMessFinPgm: .asciz "Program End ok.\n" szMessResult: .asciz "Rosetta Code => " szCarriageReturn: .asciz "\n" /* array constantes Hi */ tbConstHi: .int 0x67452301 // H0 .int 0xEFCDAB89 // H1 .int 0x98BADCFE // H2 .int 0x10325476 // H3 .int 0xC3D2E1F0 // H4 /* array constantes Kt */ tbConstKt: .int 0x5A827999 .int 0x6ED9EBA1 .int 0x8F1BBCDC .int 0xCA62C1D6 /*********************************/ /* UnInitialized data */ /*********************************/ .bss .align 4 iNbBlocs: .skip 8 sZoneConv: .skip 24 sZoneResult: .skip 24 sZoneTrav: .skip 1000 tbH: .skip 4 * 5 // 5 variables H tbW: .skip 4 * 80 // 80 words W /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrszMessRosetta //ldr x0,qAdrszMessTest1 //ldr x0,qAdrszMessTest2 //ldr x0,qAdrszMessSup64 bl computeSHA1 // call routine SHA1 ldr x0,qAdrszMessResult bl affichageMess // display message ldr x0, qAdrsZoneResult bl displaySHA1 ldr x0,qAdrszMessFinPgm bl affichageMess // display message 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrszCarriageReturn: .quad szCarriageReturn qAdrszMessResult: .quad szMessResult qAdrszMessRosetta: .quad szMessRosetta qAdrszMessTest1: .quad szMessTest1 qAdrszMessTest2: .quad szMessTest2 qAdrsZoneTrav: .quad sZoneTrav qAdrsZoneConv: .quad sZoneConv qAdrszMessFinPgm: .quad szMessFinPgm qAdrszMessSup64: .quad szMessSup64 /******************************************************************/ /* compute SHA1 */ /******************************************************************/ /* x0 contains the address of the message */ computeSHA1: stp x1,lr,[sp,-16]! // save registers ldr x1,qAdrsZoneTrav mov x2,#0 // counter length debCopy: // copy string in work area ldrb w3,[x0,x2] strb w3,[x1,x2] cmp x3,#0 add x4,x2,1 csel x2,x4,x2,ne bne debCopy lsl x6,x2,#3 // initial message length in bits mov x3,#0b10000000 // add bit 1 at end of string strb w3,[x1,x2] add x2,x2,#1 // length in bytes lsl x4,x2,#3 // length in bits mov x3,#0 addZeroes: lsr x5,x2,#6 lsl x5,x5,#6 sub x5,x2,x5 cmp x5,#56 beq storeLength // yes -> end add strb w3,[x1,x2] // add zero at message end add x2,x2,#1 // increment lenght bytes add x4,x4,#8 // increment length in bits b addZeroes storeLength: add x2,x2,#4 // add four bytes rev w6,w6 // inversion bits initials message length str w6,[x1,x2] // and store at end ldr x7,qAdrtbConstHi // constantes H address ldr x4,qAdrtbH // start area H mov x5,#0 loopConst: // init array H with start constantes ldr w6,[x7,x5,lsl #2] // load constante str w6,[x4,x5,lsl #2] // and store add x5,x5,#1 cmp x5,#5 blt loopConst // split into block of 64 bytes add x2,x2,#4 // TODO : à revoir lsr x4,x2,#6 // blocks number ldr x0,qAdriNbBlocs str x4,[x0] // save block maxi mov x7,#0 // n° de block et x1 contient l'adresse zone de travail loopBlock: // begin loop of each block of 64 bytes mov x0,x7 bl inversion // inversion each word because little indian ldr x3,qAdrtbW // working area W address mov x6,#0 // indice t /* x2 address begin each block */ ldr x1,qAdrsZoneTrav add x2,x1,x7,lsl #6 // compute block begin indice * 4 * 16 loopPrep: // loop for expand 80 words cmp x6,#15 // bgt expand1 ldr w0,[x2,x6,lsl #2] // load four byte message str w0,[x3,x6,lsl #2] // store in first 16 block b expandEnd expand1: sub x8,x6,#3 ldr w9,[x3,x8,lsl #2] sub x8,x6,#8 ldr w10,[x3,x8,lsl #2] eor x9,x9,x10 sub x8,x6,#14 ldr w10,[x3,x8,lsl #2] eor x9,x9,x10 sub x8,x6,#16 ldr w10,[x3,x8,lsl #2] eor x9,x9,x10 ror w9,w9,#31 str w9,[x3,x6,lsl #2] expandEnd: add x6,x6,#1 cmp x6,#80 // 80 words ? blt loopPrep // and loop /* COMPUTING THE MESSAGE DIGEST */ /* x1 area H constantes address */ /* x3 working area W address */ /* x5 address constantes K */ /* x6 counter t */ /* x7 block counter */ /* x8 a, x9 b, x10 c, x11 d, x12 e */ // init variable a b c d e ldr x0,qAdrtbH ldr w8,[x0] ldr w9,[x0,#4] ldr w10,[x0,#8] ldr w11,[x0,#12] ldr w12,[x0,#16] ldr x1,qAdrtbConstHi ldr x5,qAdrtbConstKt mov x6,#0 loop80T: // begin loop 80 t cmp x6,#19 bgt T2 ldr w0,[x5] // load constantes k0 and x2,x9,x10 // b and c mvn w4,w9 // not b and x4,x4,x11 // and d orr x2,x2,x4 b T_fin T2: cmp x6,#39 bgt T3 ldr w0,[x5,#4] // load constantes k1 eor x2,x9,x10 eor x2,x2,x11 b T_fin T3: cmp x6,#59 bgt T4 ldr w0,[x5,#8] // load constantes k2 and x2,x9,x10 and x4,x9,x11 orr x2,x2,x4 and x4,x10,x11 orr x2,x2,x4 b T_fin T4: ldr w0,[x5,#12] // load constantes k3 eor x2,x9,x10 eor x2,x2,x11 b T_fin T_fin: ror w4,w8,#27 // left rotate a to 5 add w2,w2,w4 //affregtit Tfin 0 //affregtit Tfin 8 add w2,w2,w12 ldr w4,[x3,x6,lsl #2] // Wt add w2,w2,w4 add w2,w2,w0 // Kt mov x12,x11 // e = d mov x11,x10 // d = c ror w10,w9,#2 // c mov x9,x8 // b = a mov x8,x2 // nouveau a add x6,x6,#1 // increment t cmp x6,#80 blt loop80T // other bloc add x7,x7,1 // increment block ldr x0,qAdriNbBlocs ldr w4,[x0] // restaur maxi block cmp x7,x4 // maxi ? bge End // End block ldr x0,qAdrtbH // start area H ldr w3,[x0] add w3,w3,w8 str w3,[x0] // store a in H0 ldr w3,[x0,#4] add w3,w3,w9 str w3,[x0,#4] // store b in H1 ldr w3,[x0,#8] add w3,w3,w10 str w3,[x0,#8] // store c in H2 ldr w3,[x0,#12] add w3,w3,w11 str w3,[x0,#12] // store d in H3 ldr w3,[x0,#16] add w3,w3,w12 str w3,[x0,#16] // store e in H4 b loopBlock // loop End: // compute final result ldr x0,qAdrtbH // start area H ldr x2,qAdrsZoneResult ldr w1,[x0] add x1,x1,x8 rev w1,w1 str w1,[x2] ldr w1,[x0,#4] add x1,x1,x9 rev w1,w1 str w1,[x2,#4] ldr w1,[x0,#8] add x1,x1,x10 rev w1,w1 str w1,[x2,#8] ldr w1,[x0,#12] add x1,x1,x11 rev w1,w1 str w1,[x2,#12] ldr w1,[x0,#16] add x1,x1,x12 rev w1,w1 str w1,[x2,#16] mov x0,#0 // routine OK 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrtbConstHi: .quad tbConstHi qAdrtbConstKt: .quad tbConstKt qAdrtbH: .quad tbH qAdrtbW: .quad tbW qAdrsZoneResult: .quad sZoneResult qAdriNbBlocs: .quad iNbBlocs /******************************************************************/ /* inversion des mots de 32 bits d'un bloc */ /******************************************************************/ /* x0 contains N° block */ inversion: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers ldr x1,qAdrsZoneTrav add x1,x1,x0,lsl 6 // debut du bloc mov x2,#0 1: // start loop ldr w3,[x1,x2,lsl #2] rev w3,w3 str w3,[x1,x2,lsl #2] add x2,x2,#1 cmp x2,#16 blt 1b 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* display hash SHA1 */ /******************************************************************/ /* x0 contains the address of hash */ displaySHA1: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x3,x0 mov x2,#0 1: ldr w0,[x3,x2,lsl #2] // load 4 bytes rev w0,w0 // reverse bytes ldr x1,qAdrsZoneConv bl conversion16_4W // conversion hexa ldr x0,qAdrsZoneConv bl affichageMess add x2,x2,#1 cmp x2,#SHA_DIGEST_LENGTH / 4 blt 1b // and loop ldr x0,qAdrszCarriageReturn bl affichageMess // display message 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* conversion hexadecimal register 32 bits */ /******************************************************************/ /* x0 contains value and x1 address zone receptrice */ conversion16_4W: stp x0,lr,[sp,-48]! // save registres stp x1,x2,[sp,32] // save registres stp x3,x4,[sp,16] // save registres mov x2,#28 // start bit position mov x4,#0xF0000000 // mask mov x3,x0 // save entry value 1: // start loop and x0,x3,x4 // value register and mask lsr x0,x0,x2 // right shift cmp x0,#10 // >= 10 ? bge 2f // yes add x0,x0,#48 // no is digit b 3f 2: add x0,x0,#55 // else is a letter A-F 3: strb w0,[x1],#1 // load result and + 1 in address lsr x4,x4,#4 // shift mask 4 bits left subs x2,x2,#4 // decrement counter 4 bits <= zero ? bge 1b // no -> loop 100: // fin standard de la fonction ldp x3,x4,[sp,16] // restaur des 2 registres ldp x1,x2,[sp,32] // restaur des 2 registres ldp x0,lr,[sp],48 // restaur des 2 registres ret /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: misc/sha256.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/SHA-256 This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program sha256.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" .equ LGHASH, 32 // result length /*******************************************/ /* Structures */ /********************************************/ /* example structure variables */ .struct 0 var_a: // a .struct var_a + 4 var_b: // b .struct var_b + 4 var_c: // c .struct var_c + 4 var_d: // d .struct var_d + 4 var_e: // e .struct var_e + 4 var_f: // f .struct var_f + 4 var_g: // g .struct var_g + 4 var_h: // h .struct var_h + 4 /*********************************/ /* Initialized data */ /*********************************/ .data szMessRosetta: .asciz "Rosetta code" szMessTest1: .asciz "abc" szMessSup64: .ascii "ABCDEFGHIJKLMNOPQRSTUVWXYZ" .ascii "abcdefghijklmnopqrstuvwxyz" .asciz "1234567890AZERTYUIOP" szMessTest2: .asciz "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" szMessFinPgm: .asciz "Program End ok.\n" szMessResult: .asciz "Rosetta code => " szCarriageReturn: .asciz "\n" /* array constantes Hi */ tbConstHi: .int 0x6A09E667 // H0 .int 0xBB67AE85 // H1 .int 0x3C6EF372 // H2 .int 0xA54FF53A // H3 .int 0x510E527F // H4 .int 0x9B05688C // H5 .int 0x1F83D9AB // H6 .int 0x5BE0CD19 // H7 /* array 64 constantes Kt */ tbConstKt: .int 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5 .int 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174 .int 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da .int 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967 .int 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85 .int 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070 .int 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 .int 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 /*********************************/ /* UnInitialized data */ /*********************************/ .bss .align 4 qNbBlocs: .skip 8 sZoneConv: .skip 24 sZoneTrav: .skip 1000 .align 8 tbH: .skip 4 * 8 // 8 variables H tbabcdefgh: .skip 4 * 8 tbW: .skip 4 * 64 // 64 words W /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrszMessRosetta //ldr x0,qAdrszMessTest1 //ldr x0,qAdrszMessTest2 //ldr x0,qAdrszMessSup64 bl computeSHA256 // call routine SHA1 ldr x0,qAdrszMessResult bl affichageMess // display message ldr x0, qAdrtbH bl displaySHA1 ldr x0,qAdrszMessFinPgm bl affichageMess // display message 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrszCarriageReturn: .quad szCarriageReturn qAdrszMessResult: .quad szMessResult qAdrszMessRosetta: .quad szMessRosetta qAdrszMessTest1: .quad szMessTest1 qAdrszMessTest2: .quad szMessTest2 qAdrsZoneTrav: .quad sZoneTrav qAdrsZoneConv: .quad sZoneConv qAdrszMessFinPgm: .quad szMessFinPgm qAdrszMessSup64: .quad szMessSup64 /******************************************************************/ /* compute SHA1 */ /******************************************************************/ /* x0 contains the address of the message */ computeSHA256: stp x1,lr,[sp,-16]! // save registers ldr x1,qAdrsZoneTrav mov x2,#0 // counter length debCopy: // copy string in work area ldrb w3,[x0,x2] strb w3,[x1,x2] cmp x3,#0 add x4,x2,1 csel x2,x4,x2,ne bne debCopy lsl x6,x2,#3 // initial message length in bits mov x3,#0b10000000 // add bit 1 at end of string strb w3,[x1,x2] add x2,x2,#1 // length in bytes lsl x4,x2,#3 // length in bits mov x3,#0 addZeroes: lsr x5,x2,#6 lsl x5,x5,#6 sub x5,x2,x5 cmp x5,#56 beq storeLength // yes -> end add strb w3,[x1,x2] // add zero at message end add x2,x2,#1 // increment lenght bytes add x4,x4,#8 // increment length in bits b addZeroes storeLength: add x2,x2,#4 // add four bytes rev w6,w6 // inversion bits initials message length str w6,[x1,x2] // and store at end ldr x7,qAdrtbConstHi // constantes H address ldr x4,qAdrtbH // start area H mov x5,#0 loopConst: // init array H with start constantes ldr w6,[x7,x5,lsl #2] // load constante str w6,[x4,x5,lsl #2] // and store add x5,x5,#1 cmp x5,#8 blt loopConst // split into block of 64 bytes add x2,x2,#4 // lsr x4,x2,#6 // blocks number ldr x0,qAdrqNbBlocs str x4,[x0] // save block maxi mov x7,#0 // n° de block et x1 contient l adresse zone de travail loopBlock: // begin loop of each block of 64 bytes mov x0,x7 bl inversion // inversion each word because little indian ldr x3,qAdrtbW // working area W address mov x6,#0 // indice t /* x2 address begin each block */ ldr x1,qAdrsZoneTrav add x2,x1,x7,lsl #6 // compute block begin indice * 4 * 16 //vidregtit avantloop //mov x0,x2 //vidmemtit verifBloc x0 10 loopPrep: // loop for expand 80 words cmp x6,#15 // bgt expand1 ldr w0,[x2,x6,lsl #2] // load word message str w0,[x3,x6,lsl #2] // store in first 16 block b expandEnd expand1: sub x8,x6,#2 ldr w9,[x3,x8,lsl #2] ror w10,w9,#17 // fonction e1 (256) ror w11,w9,#19 eor w10,w10,w11 lsr w11,w9,#10 eor w10,w10,w11 sub x8,x6,#7 ldr w9,[x3,x8,lsl #2] add w9,w9,w10 // + w - 7 sub x8,x6,#15 ldr w10,[x3,x8,lsl #2] ror w11,w10,#7 // fonction e0 (256) ror w12,w10,#18 eor w11,w11,w12 lsr w12,w10,#3 eor w10,w11,w12 add w9,w9,w10 sub x8,x6,#16 ldr w11,[x3,x8,lsl #2] add w9,w9,w11 str w9,[x3,x6,lsl #2] expandEnd: add x6,x6,#1 cmp x6,#64 // 64 words ? blt loopPrep // and loop /* COMPUTING THE MESSAGE DIGEST */ /* x1 area H constantes address */ /* x3 working area W address */ /* x5 address constantes K */ /* x6 counter t */ /* x7 block counter */ /* x8 addresse variables a b c d e f g h */ //ldr x0,qAdrtbW //vidmemtit verifW80 x0 20 // init variable a b c d e f g h ldr x0,qAdrtbH ldr x8,qAdrtbabcdefgh mov x1,#0 loopInita: ldr w9,[x0,x1,lsl #2] str w9,[x8,x1,lsl #2] add x1,x1,#1 cmp x1,#8 blt loopInita ldr x1,qAdrtbConstHi ldr x5,qAdrtbConstKt mov x6,#0 loop64T: // begin loop 64 t ldr w9,[x8,#var_h] ldr w10,[x8,#var_e] // calcul T1 ror w11,w10,#6 // fonction sigma 1 ror w12,w10,#11 eor w11,w11,w12 ror w12,w10,#25 eor w11,w11,w12 add w9,w9,w11 // h + sigma1 (e) ldr w0,[x8,#var_f] // fonction ch x and y xor (non x and z) ldr w4,[x8,#var_g] and w11,w10,w0 mvn w12,w10 and w12,w12,w4 eor w11,w11,w12 add w9,w9,w11 // h + sigma1 (e) + ch (e,f,g) ldr w0,[x5,x6,lsl #2] // load constantes k0 add w9,w9,w0 ldr w0,[x3,x6,lsl #2] // Wt add w9,w9,w0 // calcul T2 ldr w10,[x8,#var_a] // fonction sigma 0 ror w11,w10,#2 ror w12,w10,#13 eor w11,w11,w12 ror w12,w10,#22 eor w11,w11,w12 ldr w2,[x8,#var_b] ldr w4,[x8,#var_c] // fonction maj x and y xor x and z xor y and z and w12,w10,w2 and w0,w10,w4 eor w12,w12,w0 and w0,w2,w4 eor w12,w12,w0 // add w12,w12,w11 // T2 // compute variables ldr w4,[x8,#var_g] str w4,[x8,#var_h] ldr w4,[x8,#var_f] str w4,[x8,#var_g] ldr w4,[x8,#var_e] str w4,[x8,#var_f] ldr w4,[x8,#var_d] add w4,w4,w9 // add T1 str w4,[x8,#var_e] ldr w4,[x8,#var_c] str w4,[x8,#var_d] ldr w4,[x8,#var_b] str w4,[x8,#var_c] ldr w4,[x8,#var_a] str w4,[x8,#var_b] add w4,w9,w12 // add T1 T2 str w4,[x8,#var_a] add x6,x6,#1 // increment t cmp x6,#64 blt loop64T // End block ldr x0,qAdrtbH // start area H mov x10,#0 loopStoreH: ldr w9,[x8,x10,lsl #2] ldr w3,[x0,x10,lsl #2] add w3,w3,w9 str w3,[x0,x10,lsl #2] // store variables in H0 add x10,x10,#1 cmp x10,#8 blt loopStoreH // other bloc add x7,x7,#1 // increment block ldr x0,qAdrqNbBlocs ldr x4,[x0] // restaur maxi block cmp x7,x4 // maxi ? blt loopBlock // loop other block mov x0,#0 // routine OK 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrtbConstHi: .quad tbConstHi qAdrtbConstKt: .quad tbConstKt qAdrtbH: .quad tbH qAdrtbW: .quad tbW qAdrtbabcdefgh: .quad tbabcdefgh qAdrqNbBlocs: .quad qNbBlocs /******************************************************************/ /* inversion des mots de 32 bits d un bloc */ /******************************************************************/ /* x0 contains N° block */ inversion: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers ldr x1,qAdrsZoneTrav add x1,x1,x0,lsl #6 // debut du bloc mov x2,#0 1: // start loop ldr w3,[x1,x2,lsl #2] rev w3,w3 str w3,[x1,x2,lsl #2] add x2,x2,#1 cmp x2,#16 blt 1b 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* display hash SHA1 */ /******************************************************************/ /* x0 contains the address of hash */ displaySHA1: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x3,x0 mov x2,#0 1: ldr w0,[x3,x2,lsl #2] // load 4 bytes //rev x0,x0 // reverse bytes ldr x1,qAdrsZoneConv bl conversion16_4W // conversion hexa ldr x0,qAdrsZoneConv bl affichageMess add x2,x2,#1 cmp x2,#LGHASH / 4 blt 1b // and loop ldr x0,qAdrszCarriageReturn bl affichageMess // display message 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* conversion hexadecimal register 32 bits */ /******************************************************************/ /* x0 contains value and x1 address zone receptrice */ conversion16_4W: stp x0,lr,[sp,-48]! // save registres stp x1,x2,[sp,32] // save registres stp x3,x4,[sp,16] // save registres mov x2,#28 // start bit position mov x4,#0xF0000000 // mask mov x3,x0 // save entry value 1: // start loop and x0,x3,x4 // value register and mask lsr x0,x0,x2 // right shift cmp x0,#10 // >= 10 ? bge 2f // yes add x0,x0,#48 // no is digit b 3f 2: add x0,x0,#55 // else is a letter A-F 3: strb w0,[x1],#1 // load result and + 1 in address lsr x4,x4,#4 // shift mask 4 bits left subs x2,x2,#4 // decrement counter 4 bits <= zero ? bge 1b // no -> loop 100: // fin standard de la fonction ldp x3,x4,[sp,16] // restaur des 2 registres ldp x1,x2,[sp,32] // restaur des 2 registres ldp x0,lr,[sp],48 // restaur des 2 registres ret /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: misc/y_combinator.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Y_combinator This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program y_combinator.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*******************************************/ /* Structures */ /********************************************/ /* structure function*/ .struct 0 func_fn: // next element .struct func_fn + 8 func_f_: // next element .struct func_f_ + 8 func_num: .struct func_num + 8 func_fin: /* Initialized data */ .data szMessStartPgm: .asciz "Program start \n" szMessEndPgm: .asciz "Program normal end.\n" szMessError: .asciz "\033[31mError Allocation !!!\n" szFactorielle: .asciz "Function factorielle : \n" szFibonacci: .asciz "Function Fibonacci : \n" szCarriageReturn: .asciz "\n" /* datas message display */ szMessResult: .ascii "Result value : @ \n" /* UnInitialized data */ .bss sZoneConv: .skip 100 /* code section */ .text .global main main: // program start ldr x0,qAdrszMessStartPgm // display start message bl affichageMess adr x0,facFunc // function factorielle address bl YFunc // create Ycombinator mov x19,x0 // save Ycombinator ldr x0,qAdrszFactorielle // display message bl affichageMess mov x20,#1 // loop counter 1: // start loop mov x0,x20 bl numFunc // create number structure cmp x0,#-1 // allocation error ? beq 99f mov x1,x0 // structure number address mov x0,x19 // Ycombinator address bl callFunc // call ldr x0,[x0,#func_num] // load result ldr x1,qAdrsZoneConv // and convert ascii string bl conversion10S // decimal conversion ldr x0,qAdrszMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message final add x20,x20,#1 // increment loop counter cmp x20,#10 // end ? ble 1b // no -> loop /*********Fibonacci *************/ adr x0,fibFunc // function fibonacci address bl YFunc // create Ycombinator mov x19,x0 // save Ycombinator ldr x0,qAdrszFibonacci // display message bl affichageMess mov x20,#1 // loop counter 2: // start loop mov x0,x20 bl numFunc // create number structure cmp x0,#-1 // allocation error ? beq 99f mov x1,x0 // structure number address mov x0,x19 // Ycombinator address bl callFunc // call ldr x0,[x0,#func_num] // load result ldr x1,qAdrsZoneConv // and convert ascii string bl conversion10S ldr x0,qAdrszMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess add x20,x20,#1 // increment loop counter cmp x20,#10 // end ? ble 2b // no -> loop ldr x0,qAdrszMessEndPgm // display end message bl affichageMess b 100f 99: // display error message ldr x0,qAdrszMessError bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform system call qAdrszMessStartPgm: .quad szMessStartPgm qAdrszMessEndPgm: .quad szMessEndPgm qAdrszFactorielle: .quad szFactorielle qAdrszFibonacci: .quad szFibonacci qAdrszMessError: .quad szMessError qAdrszCarriageReturn: .quad szCarriageReturn qAdrszMessResult: .quad szMessResult qAdrsZoneConv: .quad sZoneConv /******************************************************************/ /* factorielle function */ /******************************************************************/ /* x0 contains the Y combinator address */ /* x1 contains the number structure */ facFunc: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // save Y combinator address ldr x0,[x1,#func_num] // load number cmp x0,#1 // > 1 ? bgt 1f // yes mov x0,#1 // create structure number value 1 bl numFunc b 100f 1: mov x3,x0 // save number sub x0,x0,#1 // decrement number bl numFunc // and create new structure number cmp x0,#-1 // allocation error ? beq 100f mov x1,x0 // new structure number -> param 1 ldr x0,[x2,#func_f_] // load function address to execute bl callFunc // call ldr x1,[x0,#func_num] // load new result mul x0,x1,x3 // and multiply by precedent bl numFunc // and create new structure number // and return her address in x0 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* fibonacci function */ /******************************************************************/ /* x0 contains the Y combinator address */ /* x1 contains the number structure */ fibFunc: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers mov x2,x0 // save Y combinator address ldr x0,[x1,#func_num] // load number cmp x0,#1 // > 1 ? bgt 1f // yes mov x0,#1 // create structure number value 1 bl numFunc b 100f 1: mov x3,x0 // save number sub x0,x0,#1 // decrement number bl numFunc // and create new structure number cmp x0,#-1 // allocation error ? beq 100f mov x1,x0 // new structure number -> param 1 ldr x0,[x2,#func_f_] // load function address to execute bl callFunc // call ldr x4,[x0,#func_num] // load new result sub x0,x3,#2 // new number - 2 bl numFunc // and create new structure number cmp x0,#-1 // allocation error ? beq 100f mov x1,x0 // new structure number -> param 1 ldr x0,[x2,#func_f_] // load function address to execute bl callFunc // call ldr x1,[x0,#func_num] // load new result add x0,x1,x4 // add two results bl numFunc // and create new structure number // and return her address in x0 100: ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* call function */ /******************************************************************/ /* x0 contains the address of the function */ /* x1 contains the address of the function 1 */ callFunc: stp x2,lr,[sp,-16]! // save registers ldr x2,[x0,#func_fn] // load function address to execute blr x2 // and call it ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* create Y combinator function */ /******************************************************************/ /* x0 contains the address of the function */ YFunc: stp x1,lr,[sp,-16]! // save registers mov x1,#0 bl newFunc cmp x0,#-1 // allocation error ? beq 100f str x0,[x0,#func_f_] // store function and return in x0 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* create structure number function */ /******************************************************************/ /* x0 contains the number */ numFunc: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // save number mov x0,#0 // function null mov x1,#0 // function null bl newFunc cmp x0,#-1 // allocation error ? beq 100f str x2,[x0,#func_num] // store number in new structure 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* new function */ /******************************************************************/ /* x0 contains the function address */ /* x1 contains the function address 1 */ newFunc: stp x1,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers stp x5,x8,[sp,-16]! // save registers mov x4,x0 // save address mov x5,x1 // save adresse 1 // allocation place on the heap mov x0,#0 // allocation place heap mov x8,BRK // call system 'brk' svc #0 mov x6,x0 // save address heap for output string add x0,x0,#func_fin // reservation place one element mov x8,BRK // call system 'brk' svc #0 cmp x0,#-1 // allocation error beq 100f mov x0,x6 str x4,[x0,#func_fn] // store address str x5,[x0,#func_f_] str xzr,[x0,#func_num] // store zero to number 100: ldp x5,x8,[sp],16 // restaur 2 registers ldp x3,x4,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/bead_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Bead_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program bead_sort.s */ /* En français tri par gravité ou tri par bille (ne pas confondre avec tri par bulle (bubble sort)) */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 #TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 TableNumber: .quad 10,9,8,7,6,5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 //.equ NBELEMENTS, 4 // for others tests /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program 1: ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl beadSort ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl isSorted // control sort cmp x0,#1 // sorted ? beq 2f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 2: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0, #0 // return code mov x8, #EXIT // request to exit program svc #0 // perform the system call qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,#0 ldr x4,[x0,x2,lsl #3] // load A[0] 1: add x2,x2,#1 cmp x2,x1 // end ? bge 99f ldr x3,[x0,x2, lsl #3] // load A[i] cmp x3,x4 // compare A[i],A[i-1] blt 98f // smaller -> error -> return mov x4,x3 // no -> A[i-1] = A[i] b 1b // and loop 98: mov x0,#0 // error b 100f 99: mov x0,#1 // ok -> return 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* bead sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of element */ /* Caution registers x2-x12 are not saved */ beadSort: stp x1,lr,[sp,-16]! // save registers mov x12,x1 // save elements number //search max ldr x10,[x0] // load value A[0] in max mov x4,#1 1: // loop search max cmp x4,x12 // end ? bge 21f // yes ldr x2,[x0,x4,lsl #3] // load value A[i] cmp x2,x10 // compare with max csel x10,x2,x10,gt // if greather add x4,x4,#1 b 1b // loop 21: mul x5,x10,x12 // max * elements number lsl x5,x5,#3 // 8 bytes for each number sub sp,sp,x5 // allocate on the stack mov fp,sp // frame pointer = stack address // marks beads mov x3,x0 // save table address mov x0,#0 // start index x 2: mov x1,#0 // index y ldr x8,[x3,x0,lsl #3] // load A[x] mul x6,x0,x10 // compute bead x 3: add x9,x6,x1 // compute bead y mov x4,#1 // value to store str x4,[fp,x9,lsl #3] // store to stack area add x1,x1,#1 cmp x1,x8 blt 3b 31: // init to zéro the bead end cmp x1,x10 // max ? bge 32f add x9,x6,x1 // compute bead y mov x4,#0 str x4,[fp,x9,lsl #3] add x1,x1,#1 b 31b 32: add x0,x0,#1 // increment x cmp x0,x12 // end ? blt 2b // count beads mov x1,#0 // y 4: mov x0,#0 // start index x mov x8,#0 // sum 5: mul x6,x0,x10 // compute bead x add x9,x6,x1 // compute bead y ldr x4,[fp,x9,lsl #3] add x8,x8,x4 mov x4,#0 str x4,[fp,x9,lsl #3] // raz bead add x0,x0,#1 cmp x0,x12 blt 5b sub x0,x12,x8 // compute end - sum 6: mul x6,x0,x10 // compute bead x add x9,x6,x1 // compute bead y mov x4,#1 str x4,[fp,x9,lsl #3] // store new bead at end add x0,x0,#1 cmp x0,x12 blt 6b add x1,x1,#1 cmp x1,x10 blt 4b // final compute mov x0,#0 // start index x 7: mov x1,#0 // start index y mul x6,x0,x10 // compute bead x 8: add x9,x6,x1 // compute bead y ldr x4,[fp,x9,lsl #3] // load bead [x,y] add x1,x1,#1 // add to x1 before str (index start at zéro) cmp x4,#1 bne 9f str x1,[x3,x0, lsl #3] // store A[x] 9: cmp x1,x10 // compare max blt 8b add x0,x0,#1 cmp x0,x12 // end ? blt 7b mov x0,#0 add sp,sp,x5 // stack alignement 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains elements number */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x4,x1 // elements number mov x3,#0 1: // loop display table ldr x0,[x2,x3,lsl #3] ldr x1,qAdrsZoneConv bl conversion10 // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv // insert conversion bl strInsertAtCharInc bl affichageMess // display message add x3,x3,#1 cmp x3,x4 // end ? blt 1b // no -> loop ldr x0,qAdrszCarriageReturn bl affichageMess 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrsZoneConv: .quad sZoneConv /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/bogo_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Bogosort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program bogo_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 qGraine: .quad 123456 TableNumber: .quad 1,2,3,4,5,6,7,8,9,10 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program 1: ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl knuthShuffle // table display elements ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl isSorted // control sort cmp x0,#1 // sorted ? bne 1b // no -> loop 100: // standard end of the program mov x0, #0 // return code mov x8, #EXIT // request to exit program svc #0 // perform the system call qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,#0 ldr x4,[x0,x2,lsl #3] // load A[0] 1: add x2,x2,#1 cmp x2,x1 // end ? bge 99f ldr x3,[x0,x2, lsl #3] // load A[i] cmp x3,x4 // compare A[i],A[i-1] blt 98f // smaller -> error -> return mov x4,x3 // no -> A[i-1] = A[i] b 1b // and loop 98: mov x0,#0 // error b 100f 99: mov x0,#1 // ok -> return 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains elements number */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x4,x1 // elements number mov x3,#0 1: // loop display table ldr x0,[x2,x3,lsl #3] ldr x1,qAdrsZoneConv bl conversion10 // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv // insert conversion bl strInsertAtCharInc bl affichageMess // display message add x3,x3,#1 cmp x3,x4 // end ? blt 1b // no -> loop ldr x0,qAdrszCarriageReturn bl affichageMess 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrsZoneConv: .quad sZoneConv /******************************************************************/ /* shuffle game */ /******************************************************************/ /* x0 contains boxs address */ /* x1 contains elements number */ knuthShuffle: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers mov x5,x0 // save table address mov x2,#0 // start index 1: mov x0,x2 // generate aleas bl genereraleas ldr x3,[x5,x2,lsl #3] // swap number on the table ldr x4,[x5,x0,lsl #3] str x4,[x5,x2,lsl #3] str x3,[x5,x0,lsl #3] add x2,x2,1 // next number cmp x2,x1 // end ? blt 1b // no -> loop 100: ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /***************************************************/ /* Generation random number */ /***************************************************/ /* x0 contains limit */ genereraleas: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers ldr x1,qAdrqGraine ldr x2,[x1] ldr x3,qNbDep1 mul x2,x3,x2 ldr x3,qNbDep2 add x2,x2,x3 str x2,[x1] // maj de la graine pour l appel suivant cmp x0,#0 beq 100f udiv x3,x2,x0 msub x0,x3,x0,x2 // résult = remainder 100: // end function ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrqGraine: .quad qGraine qNbDep1: .quad 0x0019660d qNbDep2: .quad 0x3c6ef35f /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/bubble_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program bubble_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 #TableNumber: .quad 10,9,8,7,6,5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 mov x2,NBELEMENTS // number of élements bl bubbleSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* bubble sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first element */ /* x2 contains the number of element */ bubbleSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers stp x8,x9,[sp,-16]! // save registers sub x2,x2,1 // compute i = n - 1 add x8,x1,1 1: // start loop 1 mov x3,x1 // start index mov x9,0 sub x7,x2,1 2: // start loop 2 add x4,x3,1 ldr x5,[x0,x3,lsl 3] // load value A[j] ldr x6,[x0,x4,lsl 3] // load value A[j+1] cmp x6,x5 // compare value bge 3f str x6,[x0,x3,lsl 3] // if smaller inversion str x5,[x0,x4,lsl 3] mov x9,1 // top table not sorted 3: add x3,x3,1 // increment index j cmp x3,x7 // end ? ble 2b // no -> loop 2 cmp x9,0 // table sorted ? beq 100f // yes -> end sub x2,x2,1 // decrement i cmp x2,x8 // end ? bge 1b // no -> loop 1 100: ldp x8,x9,[sp],16 // restaur 2 registers ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10 // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/circle_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_Algorithms/Circle_Sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program circle_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" szMessSortBefore: .asciz "Display table before sort.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 #TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 #TableNumber: .quad 1,2,3,4,5,6,7,8,9,10 #TableNumber: .quad 9,5,12,8,2,12,6 TableNumber: .quad 10,9,8,7,6,5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrszMessSortBefore bl affichageMess ldr x0,qAdrTableNumber // address number table bl displayTable 1: ldr x0,qAdrTableNumber // address number table mov x1,#0 mov x2,#NBELEMENTS -1 // number of élements mov x3,#0 bl circleSort cmp x0,#0 bne 1b ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl isSorted // control sort cmp x0,#1 // sorted ? beq 2f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 2: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0, #0 // return code mov x8, #EXIT // request to exit program svc #0 // perform the system call qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok qAdrszMessSortBefore: .quad szMessSortBefore /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,#0 ldr x4,[x0,x2,lsl #3] 1: add x2,x2,#1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl #3] cmp x3,x4 blt 98f // smaller -> error mov x4,x3 // A[i-1] = A[i] b 1b // else loop 98: mov x0,#0 // error b 100f 99: mov x0,#1 // ok -> return 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* circle sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first index */ /* x2 contains the last index */ /* x3 contains number of swaps */ circleSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers stp x8,x9,[sp,-16]! // save registers stp x10,x11,[sp,-16]! // save registers cmp x1,x2 beq 99f mov x7,x0 // save address mov x8,x1 // low mov x9,x2 // high sub x4,x2,x1 lsr x4,x4,#1 mov x10,x4 // mid 1: // start loop cmp x1,x2 bge 3f ldr x5,[x0,x1,lsl #3] ldr x6,[x0,x2,lsl #3] cmp x5,x6 ble 2f str x6,[x0,x1,lsl #3] // swap values str x5,[x0,x2,lsl #3] add x3,x3,#1 2: add x1,x1,#1 // increment lo sub x2,x2,#1 // decrement hi b 1b // and loop 3: cmp x1,x2 // compare lo hi bne 4f // not egal ldr x5,[x0,x1,lsl #3] add x2,x2,#1 ldr x6,[x0,x2,lsl #3] cmp x5,x6 ble 4f str x6,[x0,x1,lsl #3] // swap str x5,[x0,x2,lsl #3] add x3,x3,#1 4: mov x1,x8 // low mov x2,x10 // mid add x2,x2,x1 bl circleSort mov x3,x0 // swaps mov x0,x7 // table address mov x1,x8 // low mov x2,x10 // mid add x1,x2,x1 add x1,x1,#1 mov x2,x9 // high bl circleSort mov x3,x0 // swaps 99: mov x0,x3 // return number swaps 100: ldp x10,x11,[sp],16 // restaur 2 registers ldp x8,x9,[sp],16 // restaur 2 registers ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,#0 1: // loop display table ldr x0,[x2,x3,lsl #3] ldr x1,qAdrsZoneConv bl conversion10 // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv // insert conversion bl strInsertAtCharInc bl affichageMess // display message add x3,x3,#1 cmp x3,#NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrsZoneConv: .quad sZoneConv /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/cocktail_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program cocktail_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 #TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 mov x2,NBELEMENTS // number of élements bl cocktailSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* cocktail sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first element */ /* x2 contains the number of element */ cocktailSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers stp x8,x9,[sp,-16]! // save registers sub x2,x2,1 // compute i = n - 1 1: // start loop 1 mov x3,x1 // start index mov x9,0 sub x7,x2,1 2: // start loop 2 add x4,x3,1 ldr x5,[x0,x3,lsl 3] // load value A[j] ldr x6,[x0,x4,lsl 3] // load value A[j+1] cmp x6,x5 // compare value bge 3f str x6,[x0,x3,lsl 3] // if smaller inversion str x5,[x0,x4,lsl 3] mov x9,1 // top table not sorted 3: add x3,x3,1 // increment index j cmp x3,x7 // end ? ble 2b // no -> loop 2 cmp x9,0 // table sorted ? beq 100f // yes -> end mov x9,0 mov x3,x7 4: add x4,x3,1 ldr x5,[x0,x3,lsl 3] // load value A[j] ldr x6,[x0,x4,lsl 3] // load value A[j+1] cmp x6,x5 // compare value bge 5f str x6,[x0,x3,lsl 3] // if smaller inversion str x5,[x0,x4,lsl 3] mov x9,1 // top table not sorted 5: sub x3,x3,1 // decrement index j cmp x3,x1 // end ? bge 4b // no -> loop 2 cmp x9,0 // table sorted ? bne 1b // no -> loop 1 100: ldp x8,x9,[sp],16 // restaur 2 registers ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/comb_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Comb_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program comb_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 #TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 mov x2,NBELEMENTS // number of élements bl combSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* comb sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first element */ /* x2 contains the number of element */ /* this routine use à factor to 1.28 see wikipedia for best factor */ combSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers stp x8,x9,[sp,-16]! // save registers sub x9,x2,x1 // compute gap sub x2,x2,1 // compute end index n - 1 mov x7,100 1: // start loop 1 mul x9,x7,x9 // gap multiply by 100 lsr x9,x9,7 // divide by 128 cmp x9,0 mov x3,1 csel x9,x9,x3,ne mov x3,x1 // start index mov x8,0 // swaps 2: // start loop 2 add x4,x3,x9 // add gap to indice cmp x4,x2 bgt 4f ldr x5,[x0,x3,lsl 3] // load value A[j] ldr x6,[x0,x4,lsl 3] // load value A[j+1] cmp x6,x5 // compare value bge 3f str x6,[x0,x3,lsl 3] // if smaller inversion str x5,[x0,x4,lsl 3] mov x8,1 // swaps 3: add x3,x3,1 // increment index j b 2b 4: //bl displayTable cmp x9,1 // gap = 1 ? bne 1b // no loop cmp x8,1 // swaps ? beq 1b // yes -> loop 1 100: ldp x8,x9,[sp],16 // restaur 2 registers ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/counting_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Counting_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program counting_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 #Caution : number strictly positive and not too big TableNumber: .quad 1,3,6,2,5,9,10,8,4,5 //TableNumber: .quad 10,9,8,7,6,5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl searchMinMax mov x3,NBELEMENTS bl countSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return table address r1 return min r2 return max */ searchMinMax: stp x3,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x3,x1 // save size mov x1,1<<62 // min mov x2,0 // max mov x4,0 // index 1: ldr x5,[x0,x4,lsl 3] cmp x5,x1 csel x1,x5,x1,lt cmp x5,x2 csel x2,x5,x2,gt add x4,x4,1 cmp x4,x3 blt 1b 100: ldp x4,x5,[sp],16 // restaur 2 registers ldp x3,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* count sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the minimum */ /* x2 contains the maximum */ /* x3 contains area size */ /* caution : the count area is in the stack. if max is very large, risk of error */ countSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers stp x8,x9,[sp,-16]! // save registers sub x3,x3,1 // compute endidx = n - 1 sub x5,x2,x1 // compute max - min add x5,x5,1 // + 1 lsl x9,x5,3 // 8 bytes by number sub sp,sp,x9 // reserve count area in stack mov fp,sp // frame pointer = stack mov x6,0 mov x4,0 1: // loop init stack area str x6,[fp,x4, lsl 3] add x4,x4,#1 cmp x4,x5 blt 1b mov x4,#0 // indice 2: // start loop 2 ldr x5,[x0,x4,lsl 3] // load value A[j] sub x5,x5,x1 // - min ldr x6,[fp,x5,lsl 3] // load count of value add x6,x6,1 // increment counter str x6,[fp,x5,lsl 3] // and store add x4,x4,1 // increment indice cmp x4,x3 // end ? ble 2b // no -> loop 2 mov x7,0 // z mov x4,x1 // index = min 3: // start loop 3 sub x6,x4,x1 // compute index - min ldr x5,[fp,x6,lsl 3] // load count 4: // start loop 4 cmp x5,0 // count <> zéro beq 5f str x4,[x0,x7,lsl 3] // store value A[j] add x7,x7,1 // increment z sub x5,x5,1 // decrement count b 4b 5: add x4,x4,1 // increment index cmp x4,x2 // max ? ble 3b // no -> loop 3 add sp,sp,x9 // stack alignement 100: ldp x8,x9,[sp],16 // restaur 2 registers ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 // table address 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/gnome_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Gnome_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program gnome_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 #TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 // first element mov x2,NBELEMENTS // number of élements bl gnomeSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* gnome sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first element */ /* x2 contains the number of element */ gnomeSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers stp x8,x9,[sp,-16]! // save registers sub x2,x2,1 // compute end index n - 1 add x3,x1,1 // index i add x7,x1,2 // index j 1: // start loop 1 cmp x3,x2 bgt 100f sub x4,x3,1 // ldr x5,[x0,x3,lsl 3] // load value A[j] ldr x6,[x0,x4,lsl 3] // load value A[j+1] cmp x5,x6 // compare value bge 2f str x6,[x0,x3,lsl 3] // if smaller inversion str x5,[x0,x4,lsl 3] sub x3,x3,1 // i = i - 1 cmp x3,x1 bne 1b // loop 1 2: mov x3,x7 // i = j add x7,x7,1 // j = j + 1 b 1b // loop 1 100: ldp x8,x9,[sp],16 // restaur 2 registers ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/heap_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Heapsort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program heap_sort.s */ /* look Pseudocode begin this task */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 //TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program 1: ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl heapSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl isSorted // control sort cmp x0,#1 // sorted ? beq 2f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 2: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0, #0 // return code mov x8, #EXIT // request to exit program svc #0 // perform the system call qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,#0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* heap sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of element */ heapSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers bl heapify // first place table in max-heap order sub x3,x1,1 1: cmp x3,0 ble 100f mov x1,0 // swap the root(maximum value) of the heap with the last element of the heap) mov x2,x3 bl swapElement sub x3,x3,1 mov x1,0 mov x2,x3 // put the heap back in max-heap order bl siftDown b 1b 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* place table in max-heap order */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of element */ heapify: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers str x4,[sp,-16]! // save registers mov x4,x1 sub x3,x1,2 lsr x3,x3,1 1: cmp x3,0 blt 100f mov x1,x3 sub x2,x4,1 bl siftDown sub x3,x3,1 b 1b 100: ldr x4,[sp],16 // restaur 1 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* swap two elements of table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first index */ /* x2 contains the second index */ swapElement: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers ldr x3,[x0,x1,lsl #3] // swap number on the table ldr x4,[x0,x2,lsl #3] str x4,[x0,x1,lsl #3] str x3,[x0,x2,lsl #3] 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* put the heap back in max-heap order */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first index */ /* x2 contains the last index */ siftDown: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers // x1 = root = start mov x3,x2 // save last index 1: lsl x4,x1,1 add x4,x4,1 cmp x4,x3 bgt 100f add x5,x4,1 cmp x5,x3 bgt 2f ldr x6,[x0,x4,lsl 3] // compare elements on the table ldr x7,[x0,x5,lsl 3] cmp x6,x7 csel x4,x5,x4,lt //movlt x4,x5 2: ldr x7,[x0,x4,lsl 3] // compare elements on the table ldr x6,[x0,x1,lsl 3] // root cmp x6,x7 bge 100f mov x2,x4 // and x1 is root bl swapElement mov x1,x4 // root = child b 1b 100: ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qAdrsZoneConv: .quad sZoneConv /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/insertion_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program insertion_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 #TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 // first element mov x2,NBELEMENTS // number of élements bl insertionSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* insertion sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first element */ /* x2 contains the number of element */ insertionSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers add x3,x1,1 // index i 1: // start loop 1 ldr x4,[x0,x3,lsl 3] // load value A[i] sub x5,x3,1 // index j 2: // start loop 2 ldr x6,[x0,x5,lsl 3] // load value A[j] cmp x6,x4 // compare value ble 3f add x5,x5,1 // increment index j str x6,[x0,x5,lsl 3] // store value A[j+1} sub x5,x5,2 // j = j - 1 cmp x5,x1 // compare first element bge 2b // loop 2 3: add x5,x5,1 // increment index j str x4,[x0,x5,lsl 3] // store value A[i} add x3,x3,1 // increment index i cmp x3,x2 // end ? blt 1b // loop 1 100: ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/jort_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/JortSort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program jort_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*******************************************/ /* Initialized data */ /*******************************************/ .data szMessOk: .asciz "Ok, the list is sorted. \n" szMessNotOk: .asciz "Ouah!! this list is unsorted.\n" szCarriageReturn: .asciz "\n" tbNumber: .quad 3 .quad 4 .quad 20 .quad 5 .equ LGTBNUMBER, (. - tbNumber)/8 // number element of area /*******************************************/ /* UnInitialized data */ /*******************************************/ .bss sZoneConversion: .skip 100 .align 4 tbNumberSorted: .skip 8 * LGTBNUMBER /*******************************************/ /* code section */ /*******************************************/ .text .global main main: // entry of program ldr x0,qAdrtbNumber ldr x1,qAdrtbNumberSorted mov x2,LGTBNUMBER bl insertionSort // sort area ldr x0,qAdrtbNumber ldr x1,qAdrtbNumberSorted mov x2,LGTBNUMBER bl comparArea // control area cbz x0,1f ldr x0,qAdrszMessNotOk // not sorted bl affichageMess b 100f 1: // ok it is good ldr x0,qAdrszMessOk bl affichageMess 100: // standard end of the program mov x0, #0 // return code mov x8, #EXIT // request to exit program svc 0 // perform the system call qAdrtbNumber: .quad tbNumber qAdrtbNumberSorted: .quad tbNumberSorted qAdrszMessNotOk: .quad szMessNotOk qAdrszMessOk: .quad szMessOk qAdrszCarriageReturn: .quad szCarriageReturn /******************************************************************/ /* insertion sort */ /******************************************************************/ /* x0 contains the address of area to sort */ /* x1 contains the address of area sorted */ /* x2 contains the number of element */ insertionSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x3,0 1: // copy area unsorted to other area ldr x4,[x0,x3,lsl 3] str x4,[x1,x3,lsl 3] add x3,x3,1 cmp x3,x2 blt 1b mov x3,1 // and sort area 2: ldr x4,[x1,x3,lsl 3] subs x5,x3,1 3: cbz x5,4f ldr x6,[x1,x5,lsl 3] cmp x6,x4 ble 4f add x7,x5,1 str x6,[x1,x7,lsl 3] subs x5,x5,1 b 3b 4: add x5,x5,1 str x4,[x1,x5,lsl 3] add x3,x3,1 cmp x3,x2 blt 2b 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret /******************************************************************/ /* Comparaison elements of two areas */ /******************************************************************/ /* x0 contains the address of area to sort */ /* x1 contains the address of area sorted */ /* x2 contains the number of element */ comparArea: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x3,0 1: ldr x4,[x0,x3,lsl 3] // load element area 1 ldr x5,[x1,x3,lsl 3] // load element area 2 cmp x4,x5 // equal ? bne 99f // no -> error add x3,x3,1 // yes increment indice cmp x3,x2 // maxi ? blt 1b // no -> loop mov x0,0 // yes -> it is ok b 100f 99: mov x0,1 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/merge_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Merge_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program merge_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 TableNumber: .quad 1,3,11,6,2,5,9,10,8,4,7 #TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 // first element mov x2,NBELEMENTS // number of élements bl mergeSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* merge */ /******************************************************************/ /* r0 contains the address of table */ /* r1 contains first start index /* r2 contains second start index */ /* r3 contains the last index */ merge: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers str x8,[sp,-16]! mov x5,x2 // init index x2->x5 1: // begin loop first section ldr x6,[x0,x1,lsl 3] // load value first section index r1 ldr x7,[x0,x5,lsl 3] // load value second section index r5 cmp x6,x7 ble 4f // <= -> location first section OK str x7,[x0,x1,lsl 3] // store value second section in first section add x8,x5,1 cmp x8,x3 // end second section ? ble 2f str x6,[x0,x5,lsl 3] b 4f // loop 2: // loop insert element part 1 into part 2 sub x4,x8,1 ldr x7,[x0,x8,lsl 3] // load value 2 cmp x6,x7 // value < bge 3f str x6,[x0,x4,lsl 3] // store value b 4f // loop 3: str x7,[x0,x4,lsl 3] // store value 2 add x8,x8,1 cmp x8,x3 // end second section ? ble 2b // no loop sub x8,x8,1 str x6,[x0,x8,lsl 3] // store value 1 4: add x1,x1,1 cmp x1,x2 // end first section ? blt 1b 100: ldr x8,[sp],16 // restaur 1 register ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* merge sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the index of first element */ /* x2 contains the number of element */ mergeSort: stp x3,lr,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers cmp x2,2 // end ? blt 100f lsr x4,x2,1 // number of element of each subset add x5,x4,1 tst x2,#1 // odd ? csel x4,x5,x4,ne mov x5,x1 // save first element mov x6,x2 // save number of element mov x7,x4 // save number of element of each subset mov x2,x4 bl mergeSort mov x1,x7 // restaur number of element of each subset mov x2,x6 // restaur number of element sub x2,x2,x1 mov x3,x5 // restaur first element add x1,x1,x3 // + 1 bl mergeSort // sort first subset mov x1,x5 // restaur first element mov x2,x7 // restaur number of element of each subset add x2,x2,x1 mov x3,x6 // restaur number of element add x3,x3,x1 sub x3,x3,1 // last index bl merge 100: ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x3,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at // character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/pancake_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program pancake_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" sMessCounter: .asciz "sorted in @ flips \n" szCarriageReturn: .asciz "\n" .align 4 TableNumber: .quad 1,3,11,6,2,-5,9,10,8,4,7 #TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 // first element mov x2,NBELEMENTS // number of élements bl pancakeSort ldr x0,qAdrTableNumber // address number table bl displayTable mov x0,x10 // display counter ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessCounter ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok qAdrsMessCounter: .quad sMessCounter /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* flip */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains first start index /* x2 contains the number of elements */ /* x3 contains the position of flip */ flip: //push {r1-r6,lr} // save registers stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers str x6, [sp,-16]! // save registers add x10,x10,#1 // flips counter cmp x3,x2 sub x4,x2,1 csel x3,x4,x3,ge // last index if position >= size 1: cmp x1,x3 bge 100f ldr x5,[x0,x1,lsl 3] // load value first index ldr x6,[x0,x3,lsl 3] // load value position index str x6,[x0,x1,lsl 3] // inversion str x5,[x0,x3,lsl 3] // sub x3,x3,1 add x1,x1,1 b 1b 100: ldr x6, [sp],16 // restaur 1 register ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* pancake sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains first start index /* x2 contains the number of elements */ pancakeSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers stp x8,x9,[sp,-16]! // save registers sub x7,x2,1 // last index 1: mov x5,x1 // index mov x4,0 // max mov x3,0 // position mov x8,1 // top sorted ldr x9,[x0,x5,lsl 3] // load value A[i-1] 2: ldr x6,[x0,x5,lsl 3] // load value cmp x6,x4 // compare max csel x4,x6,x4,ge // max = A[i} csel x3,x5,x3,ge // position = index cmp x6,x9 // cmp A[i] A[i-1] sorted ? csel x8,xzr,x8,lt // no mov x9,x6 // A[i-1] = A[i] add x5,x5,1 // increment index cmp x5,x7 // end ? ble 2b cmp x8,1 // sorted ? beq 100f // yes -> end cmp x3,x7 // position ok ? beq 4f // yes cmp x3,0 // first position ? beq 3f bl flip // flip if not greather in first position 3: mov x3,x7 // and flip the whole stack bl flip 4: //bl displayTable // to display an intermediate state subs x7,x7,1 // decrement number of pancake bge 1b // and loop 100: ldp x8,x9,[sp],16 // restaur 2 registers ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/patience_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Patience_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program patience_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*******************************************/ /* Structures */ /********************************************/ /* structure Doublylinkedlist*/ .struct 0 dllist_head: // head node .struct dllist_head + 8 dllist_tail: // tail node .struct dllist_tail + 8 dllist_fin: /* structure Node Doublylinked List*/ .struct 0 NDlist_next: // next element .struct NDlist_next + 8 NDlist_prev: // previous element .struct NDlist_prev + 8 NDlist_value: // element value or key .struct NDlist_value + 8 NDlist_fin: /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 TableNumber: .quad 1,3,11,6,2,-5,9,10,8,4,7 #TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 // first element mov x2,NBELEMENTS // number of élements bl patienceSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* patience sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains first start index /* x2 contains the number of elements */ patienceSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers stp x8,x9,[sp,-16]! // save registers lsl x9,x2,1 // compute total size of piles (2 list pointer by pile ) lsl x10,x9,3 // 8 bytes by number sub sp,sp,x10 // reserve place to stack mov fp,sp // frame pointer = stack mov x3,0 // index mov x4,0 1: str x4,[fp,x3,lsl 3] // init piles area add x3,x3,1 // increment index cmp x3,x9 blt 1b mov x3,0 // index value mov x4,0 // counter first pile mov x8,x0 // save table address 2: ldr x1,[x8,x3,lsl 3] // load value add x0,fp,x4,lsl 4 // pile address bl isEmpty cmp x0,0 // pile empty ? bne 3f add x0,fp,x4,lsl 4 // pile address bl insertHead // insert value x1 b 5f 3: add x0,fp,x4,lsl 4 // pile address ldr x5,[x0,dllist_head] ldr x5,[x5,NDlist_value] // load first list value cmp x1,x5 // compare value and last value on the pile blt 4f add x0,fp,x4,lsl 4 // pile address bl insertHead // insert value x1 b 5f 4: // value is smaller créate a new pile add x4,x4,1 add x0,fp,x4,lsl 4 // pile address bl insertHead // insert value x1 5: add x3,x3,1 // increment index value cmp x3,x2 // end blt 2b // and loop /* step 2 */ mov x6,0 // index value table 6: mov x3,0 // index pile mov x5, 1<<62 // min 7: // search minimum add x0,fp,x3,lsl 4 bl isEmpty cmp x0,0 beq 8f add x0,fp,x3,lsl 4 bl searchMinList cmp x0,x5 // compare min global bge 8f mov x5,x0 // smaller -> store new min mov x7,x1 // and pointer to min add x9,fp,x3,lsl 4 // and head list 8: add x3,x3,1 // next pile cmp x3,x4 // end ? ble 7b str x5,[x8,x6,lsl 3] // store min to table value mov x0,x9 // and suppress the value in the pile mov x1,x7 bl suppressNode add x6,x6,1 // increment index value cmp x6,x2 // end ? blt 6b add sp,sp,x10 // stack alignement 100: ldp x8,x9,[sp],16 // restaur 2 registers ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at // character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* list is empty ? */ /******************************************************************/ /* x0 contains the address of the list structure */ /* x0 return 0 if empty else return 1 */ isEmpty: ldr x0,[x0,#dllist_head] cmp x0,0 cset x0,ne ret // return /******************************************************************/ /* insert value at list head */ /******************************************************************/ /* x0 contains the address of the list structure */ /* x1 contains value */ insertHead: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers mov x4,x0 // save address mov x0,x1 // value bl createNode cmp x0,#-1 // allocation error ? beq 100f ldr x2,[x4,#dllist_head] // load address first node str x2,[x0,#NDlist_next] // store in next pointer on new node mov x1,#0 str x1,[x0,#NDlist_prev] // store zero in previous pointer on new node str x0,[x4,#dllist_head] // store address new node in address head list cmp x2,#0 // address first node is null ? beq 1f str x0,[x2,#NDlist_prev] // no store adresse new node in previous pointer b 100f 1: str x0,[x4,#dllist_tail] // else store new node in tail address 100: ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* search value minimum */ /******************************************************************/ /* x0 contains the address of the list structure */ /* x0 return min */ /* x1 return address of node */ searchMinList: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers ldr x0,[x0,#dllist_head] // load first node mov x3,1<<62 mov x1,0 1: cmp x0,0 // null -> end beq 99f ldr x2,[x0,#NDlist_value] // load node value cmp x2,x3 // min ? bge 2f mov x3,x2 // value -> min mov x1,x0 // store pointer 2: ldr x0,[x0,#NDlist_next] // load addresse next node b 1b // and loop 99: mov x0,x3 // return minimum 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* suppress node */ /******************************************************************/ /* x0 contains the address of the list structure */ /* x1 contains the address to node to suppress */ suppressNode: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers ldr x2,[x1,#NDlist_next] // load addresse next node ldr x3,[x1,#NDlist_prev] // load addresse prev node cmp x3,#0 beq 1f str x2,[x3,#NDlist_next] b 2f 1: str x3,[x0,#NDlist_next] 2: cmp x2,#0 beq 3f str x3,[x2,#NDlist_prev] b 100f 3: str x2,[x0,#NDlist_prev] 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Create new node */ /******************************************************************/ /* x0 contains the value */ /* x0 return node address or -1 if allocation error*/ createNode: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x8,[sp,-16]! // save registers mov x4,x0 // save value // allocation place on the heap mov x0,0 // allocation place heap mov x8,BRK // call system 'brk' svc 0 mov x3,x0 // save address heap for output string add x0,x0,NDlist_fin // reservation place one element mov x8,BRK // call system 'brk' svc #0 cmp x0,-1 // allocation error beq 100f mov x0,x3 str x4,[x0,#NDlist_value] // store value mov x2,0 str x2,[x0,#NDlist_next] // store zero to pointer next str x2,[x0,#NDlist_prev] // store zero to pointer previous 100: ldp x4,x8,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/permutation_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program permutation_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*******************************************/ /* Structures */ /********************************************/ /* structure permutations */ .struct 0 perm_adrtable: // table value address .struct perm_adrtable + 8 perm_size: // elements number .struct perm_size + 8 perm_adrheap: // Init to zéro at the first call .struct perm_adrheap + 8 perm_end: /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessCounter: .asciz "sorted in @ permutations \n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 #TableNumber: .quad 1,3,6,2,5,9,10,8,4,7,11 TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 stPermutation: .skip perm_end /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrstPermutation // address structure permutation ldr x1,qAdrTableNumber // address number table str x1,[x0,perm_adrtable] mov x1,NBELEMENTS // elements number str x1,[x0,perm_size] mov x1,0 // first call str x1,[x0,perm_adrheap] mov x20,0 // counter 1: ldr x0,qAdrstPermutation // address structure permutation bl newPermutation // call for each permutation cmp x0,0 // end ? blt 99f // yes -> error //bl displayTable // for display after each permutation add x20,x20,1 // increment counter ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? bne 1b // no -> loop ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrszMessSortOk // address OK message bl affichageMess mov x0,x20 // display counter ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessCounter ldr x1,qAdrsZoneConv // insert conversion bl strInsertAtCharInc bl affichageMess // display message b 100f 99: ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrszMessSortNok // address not OK message bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrstPermutation: .quad stPermutation qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok qAdrsMessCounter: .quad sMessCounter /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /***************************************************/ /* return permutation one by one */ /* sur une idée de vincent Moresmau */ /* use algorytm heap iteratif see wikipedia */ /***************************************************/ /* x0 contains the address of structure permutations */ /* x0 return address of value table or zéro if end */ newPermutation: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers ldr x2,[x0,perm_adrheap] cmp x2,0 bne 2f // first call -> init area on heap mov x7,x0 ldr x1,[x7,perm_size] lsl x3,x1,3 // 8 bytes by count table add x3,x3,8 // 8 bytes for current index mov x0,0 // allocation place heap mov x8,BRK // call system 'brk' svc 0 mov x2,x0 // save address heap add x0,x0,x3 // reservation place mov x8,BRK // call system 'brk' svc #0 cmp x0,-1 // allocation error beq 100f add x8,x2,8 // address begin area counters mov x3,0 1: // loop init str xzr,[x8,x3,lsl 3] // init to zéro area heap add x3,x3,1 cmp x3,x1 blt 1b str xzr,[x2] // store zero to index str x2,[x7,perm_adrheap] // store heap address on structure permutation ldr x0,[x7,perm_adrtable] // return first permutation b 100f 2: // other calls x2 contains heap address mov x7,x0 // structure address ldr x1,[x7,perm_size] // elements number ldr x0,[x7,perm_adrtable] add x8,x2,8 // begin address area count ldr x3,[x2] // load current index 3: ldr x4,[x8,x3,lsl 3] // load count [i] cmp x4,x3 // compare with i bge 6f tst x3,#1 // even ? bne 4f ldr x5,[x0] // yes load value A[0] ldr x6,[x0,x3,lsl 3] // and swap with value A[i] str x6,[x0] str x5,[x0,x3,lsl 3] b 5f 4: ldr x5,[x0,x4,lsl 3] // no load value A[count[i]] ldr x6,[x0,x3,lsl 3] // and swap with value A[i] str x6,[x0,x4,lsl 3] str x5,[x0,x3,lsl 3] 5: add x4,x4,1 str x4,[x8,x3,lsl 3] // store new count [i] str xzr,[x2] // store new index b 100f // and return new permutation in x0 6: str xzr,[x8,x3,lsl 3] // store zero in count [i] add x3,x3,1 // increment index cmp x3,x1 // end blt 3b // loop mov x0,0 // if end -> return zero 100: // end function ldp x6,x7,[sp],16 // restaur 1 register ldp x4,x5,[sp],16 // restaur 1 register ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at // character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/quick_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Quicksort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program quick_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 TableNumber: .quad 1,3,6,2,5,9,10,8,4,7,11 #TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 // first element mov x2,NBELEMENTS // number of élements bl quickSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /***************************************************/ /* Appel récursif Tri Rapide quicksort */ /***************************************************/ /* x0 contains the address of table */ /* x1 contains index of first item */ /* x2 contains the number of elements > 0 */ quickSort: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers str x5, [sp,-16]! // save registers sub x2,x2,1 // last item index cmp x1,x2 // first > last ? bge 100f // yes -> end mov x4,x0 // save x0 mov x5,x2 // save x2 bl partition1 // cutting into 2 parts mov x2,x0 // index partition mov x0,x4 // table address bl quickSort // sort lower part add x1,x2,1 // index begin = index partition + 1 add x2,x5,1 // number of elements bl quickSort // sort higter part 100: // end function ldr x5, [sp],16 // restaur 1 register ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Partition table elements */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains index of first item */ /* x2 contains index of last item */ partition1: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers ldr x3,[x0,x2,lsl 3] // load value last index mov x4,x1 // init with first index mov x5,x1 // init with first index 1: // begin loop ldr x6,[x0,x5,lsl 3] // load value cmp x6,x3 // compare value bge 2f ldr x7,[x0,x4,lsl 3] // if < swap value table str x6,[x0,x4,lsl 3] str x7,[x0,x5,lsl 3] add x4,x4,1 // and increment index 1 2: add x5,x5,1 // increment index 2 cmp x5,x2 // end ? blt 1b // no loop ldr x7,[x0,x4,lsl 3] // swap value str x3,[x0,x4,lsl 3] str x7,[x0,x2,lsl 3] mov x0,x4 // return index partition 100: ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at // character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/radix_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Radix_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ * ARM assembly AARCH64 Raspberry PI 3B */ /* program radix_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 TableNumber: .quad 12485,301,16,25,5006,9,-154389710,26,4400,71,115 #TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 // first element mov x2,NBELEMENTS // number of élements bl radixSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* radix sort */ /******************************************************************/ /* r0 contains the address of table */ /* r1 contains the first element */ /* r2 contains the number of element */ /* no registers save */ radixSort: str lr,[sp,-16]! // save 1 register mov x7,0b1111 // mask one digit hexa mov x10,0 // digit counter 1: add x3,x1,1 // start index i 2: // start loop ldr x4,[x0,x3,lsl 3] // load value A[i] and x8,x4,x7 // and mask sub x5,x3,1 // index j 3: ldr x6,[x0,x5,lsl 3] // load value A[j] and x9,x6,x7 // and mask cmp x9,x8 // compare one digit hexa ble 4f add x5,x5,1 // increment index j str x6,[x0,x5,lsl 3] // store value A[j+1] sub x5,x5,2 // j = j - 1 cmp x5,x1 bge 3b // loop if j >= first item 4: add x5,x5,1 // increment index j str x4,[x0,x5,lsl 3] // store value A[i] in A[j+1] add x3,x3,1 // increment index i cmp x3,x2 // end ? blt 2b // no -> loop //bl displayTable lsl x7,x7,4 // shift mask 4 bits left add x10,x10,1 // increment counter cmp x10,16 // 16 digits ? blt 1b // no loop 100: ldr lr,[sp],16 // restaur 1 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10S // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at // character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess mov x0,x2 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/selection_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program selection_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 #TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 TableNumber: .quad 10,9,8,7,6,5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 mov x2,NBELEMENTS // number of élements bl selectionSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* selection sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first element */ /* x2 contains the number of element */ selectionSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers mov x3,x1 // start index i sub x7,x2,1 // compute n - 1 1: // start loop mov x4,x3 add x5,x3,1 // init index 2 2: ldr x1,[x0,x4,lsl 3] // load value A[mini] ldr x6,[x0,x5,lsl 3] // load value A[j] cmp x6,x1 // compare value csel x4,x5,x4,lt // j -> mini add x5,x5,1 // increment index j cmp x5,x2 // end ? blt 2b // no -> loop cmp x4,x3 // mini <> j ? beq 3f // no ldr x1,[x0,x4,lsl 3] // yes swap A[i] A[mini] ldr x6,[x0,x3,lsl 3] str x1,[x0,x3,lsl 3] str x6,[x0,x4,lsl 3] 3: add x3,x3,1 // increment i cmp x3,x7 // end ? blt 1b // no -> loop 100: ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10 // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: sorters/shell_sort.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Sorting_algorithms/Shell_sort This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program shell_sort.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value : @ \n" szCarriageReturn: .asciz "\n" .align 4 TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 //TableNumber: .quad 10,9,8,7,6,5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program 1: ldr x0,qAdrTableNumber // address number table mov x1,0 // not use in routine mov x2,NBELEMENTS // number of élements bl shellSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,#NBELEMENTS // number of élements bl isSorted // control sort cmp x0,#1 // sorted ? beq 2f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 2: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // error not sorted b 100f 99: mov x0,1 // sorted 100: ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /***************************************************/ /* shell Sort */ /***************************************************/ /* x0 contains the address of table */ /* x1 contains the first element but not use !! */ /* this routine use first element at index zero !!! */ /* x2 contains the number of element */ shellSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers sub x2,x2,1 // index last item mov x1,x2 // init gap = last item 1: // start loop 1 lsr x1,x1,1 // gap = gap / 2 cbz x1,100f // if gap = 0 -> end mov x3,x1 // init loop indice 1 2: // start loop 2 ldr x4,[x0,x3,lsl 3] // load first value mov x5,x3 // init loop indice 2 3: // start loop 3 cmp x5,x1 // indice < gap blt 4f // yes -> end loop 2 sub x6,x5,x1 // index = indice - gap ldr x7,[x0,x6,lsl 3] // load second value cmp x4,x7 // compare values bge 4f str x7,[x0,x5,lsl 3] // store if < sub x5,x5,x1 // indice = indice - gap b 3b // and loop 4: // end loop 3 str x4,[x0,x5,lsl 3] // store value 1 at indice 2 add x3,x3,1 // increment indice 1 cmp x3,x2 // end ? ble 2b // no -> loop 2 b 1b // yes loop for new gap 100: // end function ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl #3] ldr x1,qAdrsZoneConv // display value bl conversion10 // call function ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,#NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: string/append.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/String_append This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program append.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" .equ BUFFERSIZE, 100 /*******************************************/ /* Initialized data */ /*******************************************/ .data szMessString: .asciz "String :\n" szString1: .asciz "Alphabet : " sComplement: .fill BUFFERSIZE,1,0 szString2: .asciz "abcdefghijklmnopqrstuvwxyz" szCarriageReturn: .asciz "\n" /*******************************************/ /* UnInitialized data */ /*******************************************/ .bss /*******************************************/ /* code section */ /*******************************************/ .text .global main main: ldr x0,qAdrszMessString // display message bl affichageMess ldr x0,qAdrszString1 // display begin string bl affichageMess ldr x0,qAdrszCarriageReturn // display return line bl affichageMess ldr x0,qAdrszString1 ldr x1,qAdrszString2 bl append // append sting2 to string1 ldr x0,qAdrszMessString bl affichageMess ldr x0,qAdrszString1 // display string bl affichageMess ldr x0,qAdrszCarriageReturn bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform system call qAdrszMessString: .quad szMessString qAdrszString1: .quad szString1 qAdrszString2: .quad szString2 qAdrszCarriageReturn: .quad szCarriageReturn /**************************************************/ /* append two strings */ /**************************************************/ /* x0 contains the address of the string1 */ /* x1 contains the address of the string2 */ append: stp x1,lr,[sp,-16]! // save registers mov x2,#0 // counter byte string 1 1: ldrb w3,[x0,x2] // load byte string 1 cmp x3,#0 // zero final ? add x4,x2,1 csel x2,x4,x2,ne // if x3 not equal 0, x2 = X2 +1 else x2 bne 1b // no -> loop mov x4,#0 // counter byte string 2 2: ldrb w3,[x1,x4] // load byte string 2 strb w3,[x0,x2] // store byte string 1 cbz x3,100f // zero final ? add x2,x2,1 // no -> increment counter 1 add x4,x4,1 // no -> increment counter 2 b 2b // no -> loop 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: string/comparison.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/String_comparison This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program comparison.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*******************************************/ /* Initialized data */ /*******************************************/ .data szMessStringEqu: .asciz "The strings are equals.\n" szMessStringNotEqu: .asciz "The strings are not equals.\n" szCarriageReturn: .asciz "\n" szString1: .asciz "ABCDE" szString2: .asciz "ABCDE" szString3: .asciz "ABCFG" szString4: .asciz "ABC" szString5: .asciz "abcde" /*******************************************/ /* UnInitialized data / /*******************************************/ .bss /*******************************************/ /* code section */ /*******************************************/ .text .global main main: // entry of program ldr x0,qAdrszString1 ldr x1,qAdrszString2 bl Comparaison ldr x0,qAdrszString1 ldr x1,qAdrszString3 bl Comparaison ldr x0,qAdrszString1 ldr x1,qAdrszString4 bl Comparaison // case sensitive comparisons ABCDE et abcde ldr x0,qAdrszString1 ldr x1,qAdrszString5 bl Comparaison // case insensitive comparisons ABCDE et abcde ldr x0,qAdrszString1 ldr x1,qAdrszString5 bl comparStringsInsensitive cbnz x0,1f ldr x0,qAdrszMessStringEqu bl affichageMess b 2f 1: ldr x0,qAdrszMessStringNotEqu bl affichageMess 2: 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrszString1: .quad szString1 qAdrszString2: .quad szString2 qAdrszString3: .quad szString3 qAdrszString4: .quad szString4 qAdrszString5: .quad szString5 qAdrszMessStringEqu: .quad szMessStringEqu qAdrszMessStringNotEqu: .quad szMessStringNotEqu qAdrszCarriageReturn: .quad szCarriageReturn /*********************************************/ /* comparaison */ /*********************************************/ /* x0 contains address String 1 */ /* x1 contains address String 2 */ Comparaison: stp x1,lr,[sp,-16]! // save registers bl comparStrings cbnz x0,1f ldr x0,qAdrszMessStringEqu bl affichageMess b 2f 1: ldr x0,qAdrszMessStringNotEqu bl affichageMess 2: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /************************************/ /* Strings case sensitive comparisons */ /************************************/ /* x0 et x1 contains the address of strings */ /* return 0 in x0 if equals */ /* return -1 if string x0 < string x1 */ /* return 1 if string x0 > string x1 */ comparStrings: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers mov x2,#0 // counter 1: ldrb w3,[x0,x2] // byte string 1 ldrb w4,[x1,x2] // byte string 2 cmp x3,x4 blt 2f bgt 3f cbz x3,4f // 0 end string add x2,x2,1 // else add 1 in counter b 1b // and loop */ 2: mov x0,-1 // lower b 100f 3: mov x0,1 // higher b 100f 4: mov x0,0 // equal 100: ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /************************************/ /* Strings case insensitive comparisons */ /************************************/ /* x0 et x1 contains the address of strings */ /* return 0 in x0 if equals */ /* return -1 if string x0 < string x1 */ /* return 1 if string x0 > string x1 */ comparStringsInsensitive: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers mov x2,#0 // counter 1: ldrb w3,[x0,x2] // byte string 1 ldrb w4,[x1,x2] // byte string 2 // majuscules --> minuscules byte 1 cmp x3,65 blt 2f cmp x3,90 bgt 2f add x3,x3,32 2: // majuscules --> minuscules byte 2 cmp x4,65 blt 3f cmp x4,90 bgt 3f add x4,x4,32 3: cmp x3,x4 blt 4f bgt 5f cbz x3,6f // 0 end string add x2,x2,1 // else add 1 in counter b 1b // and loop 4: mov x0,-1 // lower b 100f 5: mov x0,1 // higher b 100f 6: mov x0,0 // equal 100: ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: string/concatenation.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/String_concatenation This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program concatenation.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*******************************************/ /* Initialized data */ /*******************************************/ .data szMessFinal: .asciz "The final string is \n" szString: .asciz "Hello " szString1: .asciz " the world. \n" /*******************************************/ /* UnInitialized data */ /*******************************************/ .bss szFinalString: .skip 255 /*******************************************/ /* code section */ /*******************************************/ .text .global main main: // load string ldr x1,qAdrszString ldr x2,qAdrszFinalString mov x4,0 1: ldrb w0,[x1,x4] // load byte of string strb w0,[x2,x4] cmp x0,0 // compar with zero ? add x3,x4,1 csel x4,x3,x4,ne // if x0 <> 0 x4 = x4 +1 sinon x4 bne 1b ldr x1,qAdrszString1 mov x3,0 2: ldrb w0,[x1,x3] // load byte of string 1 strb w0,[x2,x4] cmp x0,0 // compar with zero ? add x5,x4,1 csel x4,x5,x4,ne add x5,x3,1 csel x3,x5,x3,ne bne 2b mov x0,x2 // display final string bl affichageMess 100: // standard end of the program */ mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrszString: .quad szString qAdrszString1: .quad szString1 qAdrszFinalString: .quad szFinalString qAdrszMessFinal: .quad szMessFinal /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: string/interpolation.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/String_interpolation_(included) This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program interpolation.s */ /* In assembler, there is no function to insert a chain */ /* so this program offers two functions to insert */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" .equ CHARPOS, '@' /*******************************************/ /* Initialized data */ /*******************************************/ .data szString: .asciz " string " szString1: .asciz "insert" szString2: .asciz "abcd@efg" szString3: .asciz "abcdef @" szString4: .asciz "@ abcdef" szCarriageReturn: .asciz "\n" /*******************************************/ /* UnInitialized data */ /*******************************************/ .bss /*******************************************/ /* code section */ /*******************************************/ .text .global main main: // entry of program ldr x0,qAdrszString // string address ldr x1,qAdrszString1 // string address mov x2,#0 bl strInsert // // return new pointer bl affichageMess // display result string ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszString // string address ldr x1,qAdrszString1 // string address mov x2,#3 bl strInsert // // return new pointer bl affichageMess // display result string ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszString // string address ldr x1,qAdrszString1 // string address mov x2,#40 bl strInsert // // return new pointer bl affichageMess // display result string ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszString2 // string address ldr x1,qAdrszString1 // string address bl strInsertAtChar // // return new pointer bl affichageMess // display result string ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszString3 // string address ldr x1,qAdrszString1 // string address bl strInsertAtChar // // return new pointer bl affichageMess // display result string ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszString4 // string address ldr x1,qAdrszString1 // string address bl strInsertAtChar // // return new pointer bl affichageMess // display result string ldr x0,qAdrszCarriageReturn bl affichageMess 100: // standard end of the program mov x0, #0 // return code mov x8, #EXIT // request to exit program svc 0 // perform the system call qAdrszString: .quad szString qAdrszString1: .quad szString1 qAdrszString2: .quad szString2 qAdrszString3: .quad szString3 qAdrszString4: .quad szString4 qAdrszCarriageReturn: .quad szCarriageReturn /******************************************************************/ /* insertion of a sub-chain in a chain in the desired position */ /******************************************************************/ /* x0 contains the address of string 1 */ /* x1 contains the address of string to insert */ /* x2 contains the position of insertion : 0 start string if x2 > lenght string 1 insert at end of string*/ /* x0 return the address of new string on the heap */ strInsert: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x3,#0 // length counter 1: // compute length of string 1 ldrb w4,[x0,x3] cmp w4,#0 cinc x3,x3,ne // increment to one if not equal bne 1b // loop if not equal mov x5,#0 // length counter insertion string 2: // compute length of insertion string ldrb w4,[x1,x5] cmp x4,#0 cinc x5,x5,ne // increment to one if not equal bne 2b cmp x5,#0 beq 99f // string empty -> error add x3,x3,x5 // add 2 length add x3,x3,#1 // +1 for final zero mov x6,x0 // save address string 1 mov x0,#0 // allocation place heap mov x8,BRK // call system 'brk' svc #0 mov x5,x0 // save address heap for output string add x0,x0,x3 // reservation place x3 length mov x8,BRK // call system 'brk' svc #0 cmp x0,#-1 // allocation error beq 99f // mov x8,#0 // index load characters string 1 cmp x2,#0 // index insertion = 0 beq 5f // insertion at string 1 begin 3: // loop copy characters string 1 ldrb w0,[x6,x8] // load character cmp w0,#0 // end string ? beq 5f // insertion at end strb w0,[x5,x8] // store character in output string add x8,x8,#1 // increment index cmp x8,x2 // < insertion index ? blt 3b // yes -> loop 5: mov x4,x8 // init index character output string mov x3,#0 // index load characters insertion string 6: ldrb w0,[x1,x3] // load characters insertion string cmp w0,#0 // end string ? beq 7f strb w0,[x5,x4] // store in output string add x3,x3,#1 // increment index add x4,x4,#1 // increment output index b 6b // and loop 7: ldrb w0,[x6,x8] // load other character string 1 strb w0,[x5,x4] // store in output string cmp x0,#0 // end string 1 ? beq 8f // yes -> end add x4,x4,#1 // increment output index add x8,x8,#1 // increment index b 7b // and loop 8: mov x0,x5 // return output string address b 100f 99: // error mov x0,#-1 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret /******************************************************************/ /* insert string at character insertion */ /******************************************************************/ /* x0 contains the address of string 1 */ /* x1 contains the address of insertion string */ /* x0 return the address of new string on the heap */ /* or -1 if error */ strInsertAtChar: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x3,#0 // length counter 1: // compute length of string 1 ldrb w4,[x0,x3] cmp w4,#0 cinc x3,x3,ne // increment to one if not equal bne 1b // loop if not equal mov x5,#0 // length counter insertion string 2: // compute length to insertion string ldrb w4,[x1,x5] cmp x4,#0 cinc x5,x5,ne // increment to one if not equal bne 2b // and loop cmp x5,#0 beq 99f // string empty -> error add x3,x3,x5 // add 2 length add x3,x3,#1 // +1 for final zero mov x6,x0 // save address string 1 mov x0,#0 // allocation place heap mov x8,BRK // call system 'brk' svc #0 mov x5,x0 // save address heap for output string add x0,x0,x3 // reservation place x3 length mov x8,BRK // call system 'brk' svc #0 cmp x0,#-1 // allocation error beq 99f mov x2,0 mov x4,0 3: // loop copy string begin ldrb w3,[x6,x2] cmp w3,0 beq 99f cmp w3,CHARPOS // insertion character ? beq 5f // yes strb w3,[x5,x4] // no store character in output string add x2,x2,1 add x4,x4,1 b 3b // and loop 5: // x4 contains position insertion add x8,x4,1 // init index character output string // at position insertion + one mov x3,#0 // index load characters insertion string 6: ldrb w0,[x1,x3] // load characters insertion string cmp w0,#0 // end string ? beq 7f // yes strb w0,[x5,x4] // store in output string add x3,x3,#1 // increment index add x4,x4,#1 // increment output index b 6b // and loop 7: // loop copy end string ldrb w0,[x6,x8] // load other character string 1 strb w0,[x5,x4] // store in output string cmp x0,#0 // end string 1 ? beq 8f // yes -> end add x4,x4,#1 // increment output index add x8,x8,#1 // increment index b 7b // and loop 8: mov x0,x5 // return output string address b 100f 99: // error mov x0,#-1 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: string/length.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/String_length This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program length.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data sMessResultByte: .asciz "===Byte Length=== : @ \n" sMessResultChar: .asciz "===Character Length=== : @ \n" szString1: .asciz "møøse€" szCarriageReturn: .asciz "\n" /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrszString1 bl affichageMess // display string ldr x0,qAdrszCarriageReturn bl affichageMess ldr x0,qAdrszString1 mov x1,#0 1: // loop compute length bytes ldrb w2,[x0,x1] cmp w2,#0 cinc x1,x1,ne bne 1b mov x0,x1 // result display ldr x1,qAdrsZoneConv bl conversion10 // call decimal conversion ldr x0,qAdrsMessResultByte ldr x1,qAdrsZoneConv // insert conversion in message bl strInsertAtCharInc bl affichageMess ldr x0,qAdrszString1 mov x1,#0 mov x3,#0 2: // loop compute length characters ldrb w2,[x0,x1] cmp w2,#0 beq 6f and x2,x2,#0b11100000 // 3 bytes ? cmp x2,#0b11100000 bne 3f add x3,x3,#1 add x1,x1,#3 b 2b 3: and x2,x2,#0b11000000 // 2 bytes ? cmp x2,#0b11000000 bne 4f add x3,x3,#1 add x1,x1,#2 b 2b 4: // else 1 byte add x3,x3,#1 add x1,x1,#1 b 2b 6: mov x0,x3 ldr x1,qAdrsZoneConv bl conversion10 // call decimal conversion ldr x0,qAdrsMessResultChar ldr x1,qAdrsZoneConv // insert conversion in message bl strInsertAtCharInc bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResultByte: .quad sMessResultByte qAdrsMessResultChar: .quad sMessResultChar qAdrszString1: .quad szString1 qAdrsZoneConv: .quad sZoneConv /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: string/matching.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/String_matching This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program matching.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*******************************************/ /* Initialized data */ /*******************************************/ .data szMessFound: .asciz "String found. \n" szMessNotFound: .asciz "String not found. \n" szString: .asciz "abcdefghijklmnopqrstuvwxyz" szString2: .asciz "abc" szStringStart: .asciz "abcd" szStringEnd: .asciz "xyz" szStringStart2: .asciz "abcd" szStringEnd2: .asciz "xabc" szCarriageReturn: .asciz "\n" /*******************************************/ /* UnInitialized data */ /*******************************************/ .bss /*******************************************/ /* code section */ /*******************************************/ .text .global main main: ldr x0,qAdrszString // address input string ldr x1,qAdrszStringStart // address search string bl searchStringDeb // Determining if the first string starts with second string cmp x0,0 ble 1f ldr x0,qAdrszMessFound // display message bl affichageMess b 2f 1: ldr x0,qAdrszMessNotFound bl affichageMess 2: ldr x0,qAdrszString // address input string ldr x1,qAdrszStringEnd // address search string bl searchStringFin // Determining if the first string ends with the second string cmp x0,0 ble 3f ldr x0,qAdrszMessFound // display message bl affichageMess b 4f 3: ldr x0,qAdrszMessNotFound bl affichageMess 4: ldr x0,qAdrszString2 // address input string ldr x1,qAdrszStringStart2 // address search string bl searchStringDeb // cmp x0,0 ble 5f ldr x0,qAdrszMessFound // display message bl affichageMess b 6f 5: ldr x0,qAdrszMessNotFound bl affichageMess 6: ldr x0,qAdrszString2 // address input string ldr x1,qAdrszStringEnd2 // address search string bl searchStringFin cmp x0,0 ble 7f ldr x0,qAdrszMessFound // display message bl affichageMess b 8f 7: ldr x0,qAdrszMessNotFound bl affichageMess 8: ldr x0,qAdrszString // address input string ldr x1,qAdrszStringEnd // address search string bl searchSubString // Determining if the first string contains the second string at any location cmp x0,0 ble 9f ldr x0,qAdrszMessFound // display message bl affichageMess b 10f 9: ldr x0,qAdrszMessNotFound // display substring result bl affichageMess 10: 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform system call qAdrszMessFound: .quad szMessFound qAdrszMessNotFound: .quad szMessNotFound qAdrszString: .quad szString qAdrszString2: .quad szString2 qAdrszStringStart: .quad szStringStart qAdrszStringEnd: .quad szStringEnd qAdrszStringStart2: .quad szStringStart2 qAdrszStringEnd2: .quad szStringEnd2 qAdrszCarriageReturn: .quad szCarriageReturn /******************************************************************/ /* search substring at begin of input string */ /******************************************************************/ /* x0 contains the address of the input string */ /* x1 contains the address of substring */ /* x0 returns 1 if find or 0 if not or -1 if error */ searchStringDeb: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x3,0 // counter byte string ldrb w4,[x1,x3] // load first byte of substring cbz x4,99f // empty string ? 1: ldrb w2,[x0,x3] // load byte string input cbz x2,98f // zero final ? cmp x4,x2 // bytes equals ? bne 98f // no not find add x3,x3,1 // increment counter ldrb w4,[x1,x3] // and load next byte of substring cbnz x4,1b // zero final ? mov x0,1 // yes is ok b 100f 98: mov x0,0 // not find b 100f 99: mov x0,-1 // error 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* search substring at end of input string */ /******************************************************************/ /* x0 contains the address of the input string */ /* x1 contains the address of substring */ /* x0 returns 1 if find or 0 if not or -1 if error */ searchStringFin: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers mov x3,0 // counter byte string // search the last character of substring 1: ldrb w4,[x1,x3] // load byte of substring cmp x4,#0 // zero final ? add x2,x3,1 csel x3,x2,x3,ne // no increment counter //addne x3,#1 // no increment counter bne 1b // and loop cbz x3,99f // empty string ? sub x3,x3,1 // index of last byte ldrb w4,[x1,x3] // load last byte of substring // search the last character of string mov x2,0 // index last character 2: ldrb w5,[x0,x2] // load first byte of substring cmp x5,0 // zero final ? add x5,x2,1 // no -> increment counter csel x2,x5,x2,ne //addne x2,#1 // no -> increment counter bne 2b // and loop cbz x2,98f // empty input string ? sub x2,x2,1 // index last character 3: ldrb w5,[x0,x2] // load byte string input cmp x4,x5 // bytes equals ? bne 98f // no -> not found subs x3,x3,1 // decrement counter blt 97f // ok found subs x2,x2,1 // decrement counter input string blt 98f // if zero -> not found ldrb w4,[x1,x3] // load previous byte of substring b 3b // and loop 97: mov x0,1 // yes is ok b 100f 98: mov x0,0 // not found b 100f 99: mov x0,-1 // error 100: ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* search a substring in the string */ /******************************************************************/ /* x0 contains the address of the input string */ /* x1 contains the address of substring */ /* x0 returns index of substring in string or -1 if not found */ searchSubString: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers mov x2,0 // counter byte input string mov x3,0 // counter byte string mov x6,-1 // index found ldrb w4,[x1,x3] 1: ldrb w5,[x0,x2] // load byte string cbz x5,99f // zero final ? cmp x5,x4 // compare character beq 2f mov x6,-1 // no equals - > raz index mov x3,0 // and raz counter byte add x2,x2,1 // and increment counter byte b 1b // and loop 2: // characters equals cmp x6,-1 // first characters equals ? csel x6,x2,x6,eq // yes -> index begin in x6 //moveq x6,x2 // yes -> index begin in x6 add x3,x3,1 // increment counter substring ldrb w4,[x1,x3] // and load next byte cmp x4,0 // zero final ? beq 3f // yes -> end search add x2,x2,1 // else increment counter string b 1b // and loop 3: mov x0,x6 b 100f 98: mov x0,0 // not found b 100f 99: mov x0,-1 // error 100: ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: string/prepend.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/String_prepend This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program prepend.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*******************************************/ /* Initialized data */ /*******************************************/ .data szMessString: .asciz "British Museum.\n" szComplement: .skip 80 szStringStart: .asciz "The rosetta stone is at " szCarriageReturn: .asciz "\n" /*******************************************/ /* UnInitialized data */ /*******************************************/ .bss /*******************************************/ /* code section */ /*******************************************/ .text .global main main: ldr x0,qAdrszMessString // display message bl affichageMess ldr x0,qAdrszMessString ldr x1,qAdrszStringStart bl prepend // append sting2 to string1 ldr x0,qAdrszMessString bl affichageMess ldr x0,qAdrszCarriageReturn bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform system call qAdrszMessString: .quad szMessString qAdrszStringStart: .quad szStringStart qAdrszCarriageReturn: .quad szCarriageReturn /**************************************************/ /* append two strings */ /**************************************************/ /* x0 contains the address of the string1 */ /* x1 contains the address of the string2 */ prepend: stp x1,lr,[sp,-16]! // save registers mov x3,#0 // length counter 1: // compute length of string 1 ldrb w4,[x0,x3] cmp w4,#0 cinc x3,x3,ne // increment to one if not equal bne 1b // loop if not equal mov x5,#0 // length counter insertion string 2: // compute length of insertion string ldrb w4,[x1,x5] cmp x4,#0 cinc x5,x5,ne // increment to one if not equal bne 2b cmp x5,#0 beq 99f // string empty -> error add x3,x3,x5 // add 2 length add x3,x3,#1 // +1 for final zero mov x6,x0 // save address string 1 mov x0,#0 // allocation place heap mov x8,BRK // call system 'brk' svc #0 mov x5,x0 // save address heap for output string add x0,x0,x3 // reservation place x3 length mov x8,BRK // call system 'brk' svc #0 cmp x0,#-1 // allocation error beq 99f mov x4,#0 // counter byte string 2 3: ldrb w3,[x1,x4] // load byte string 2 cbz x3,4f // zero final ? strb w3,[x5,x4] // store byte string 2 in heap add x4,x4,1 // increment counter 1 b 3b // no -> loop 4: mov x2,#0 // counter byte string 1 5: ldrb w3,[x6,x2] // load byte string 1 strb w3,[x5,x4] // store byte string in heap cbz x3,6f // zero final ? add x2,x2,1 // no -> increment counter 1 add x4,x4,1 // no -> increment counter 2 b 5b // no -> loop 6: // recopie heap in string 1 mov x2,#0 // counter byte string 7: ldrb w3,[x5,x2] // load byte string in heap strb w3,[x6,x2] // store byte string 1 cbz x3,100f // zero final ? add x2,x2,1 // no -> increment counter 1 b 7b // no -> loop 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: string/substring.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Substring This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program substring.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" /*******************************************/ /* Initialized data */ /*******************************************/ .data szMessString: .asciz "Result : " szString1: .asciz "abcdefghijklmnopqrstuvwxyz" szStringStart: .asciz "abcdefg" szCarriageReturn: .asciz "\n" /*******************************************/ /* UnInitialized data */ /*******************************************/ .bss szSubString: .skip 500 // buffer result /*******************************************/ /* code section */ /*******************************************/ .text .global main main: ldr x0,qAdrszString1 // address input string ldr x1,qAdrszSubString // address output string mov x2,22 // location mov x3,4 // length bl subStringNbChar // starting from n characters in and of m length ldr x0,qAdrszMessString // display message bl affichageMess ldr x0,qAdrszSubString // display substring result bl affichageMess ldr x0,qAdrszCarriageReturn // display line return bl affichageMess // ldr x0,qAdrszString1 ldr x1,qAdrszSubString mov x2,15 // location bl subStringEnd //starting from n characters in, up to the end of the string ldr x0,qAdrszMessString // display message bl affichageMess ldr x0,qAdrszSubString bl affichageMess ldr x0,qAdrszCarriageReturn // display line return bl affichageMess // ldr x0,qAdrszString1 ldr x1,qAdrszSubString bl subStringMinus // whole string minus last character ldr x0,qAdrszMessString // display message bl affichageMess ldr x0,qAdrszSubString bl affichageMess ldr x0,qAdrszCarriageReturn // display line return bl affichageMess // ldr x0,qAdrszString1 ldr x1,qAdrszSubString mov x2,'c' // start character mov x3,5 // length bl subStringStChar //starting from a known character within the string and of m length cmp x0,-1 // error ? beq 2f ldr x0,qAdrszMessString // display message bl affichageMess ldr x0,qAdrszSubString bl affichageMess ldr x0,qAdrszCarriageReturn // display line return bl affichageMess // 2: ldr x0,qAdrszString1 ldr x1,qAdrszSubString ldr x2,qAdrszStringStart // sub string to start mov x3,10 // length bl subStringStString // starting from a known substring within the string and of m length cmp x0,-1 // error ? beq 3f ldr x0,qAdrszMessString // display message bl affichageMess ldr x0,qAdrszSubString bl affichageMess ldr x0,qAdrszCarriageReturn // display line return bl affichageMess 3: 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform system call qAdrszMessString: .quad szMessString qAdrszString1: .quad szString1 qAdrszSubString: .quad szSubString qAdrszStringStart: .quad szStringStart qAdrszCarriageReturn: .quad szCarriageReturn /******************************************************************/ /* sub strings index start number of characters */ /******************************************************************/ /* x0 contains the address of the input string */ /* x1 contains the address of the output string */ /* x2 contains the start index */ /* x3 contains numbers of characters to extract */ /* x0 returns number of characters or -1 if error */ subStringNbChar: stp x1,lr,[sp,-16]! // save registers mov x14,#0 // counter byte output string 1: ldrb w15,[x0,x2] // load byte string input cbz x15,2f // zero final ? strb w15,[x1,x14] // store byte output string add x2,x2,1 // increment counter add x14,x14,1 cmp x14,x3 // end ? blt 1b // no -> loop 2: strb wzr,[x1,x14] // store final zero byte string 2 mov x0,x14 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* sub strings index start at end of string */ /******************************************************************/ /* x0 contains the address of the input string */ /* x1 contains the address of the output string */ /* x2 contains the start index */ /* x0 returns number of characters or -1 if error */ subStringEnd: stp x2,lr,[sp,-16]! // save registers mov x14,0 // counter byte output string 1: ldrb w15,[x0,x2] // load byte string 1 cbz x15,2f // zero final ? strb w15,[x1,x14] add x2,x2,1 add x14,x14,1 b 1b // loop 2: strb wzr,[x1,x14] // store final zero byte string 2 mov x0,x14 100: ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* whole string minus last character */ /******************************************************************/ /* x0 contains the address of the input string */ /* x1 contains the address of the output string */ /* x0 returns number of characters or -1 if error */ subStringMinus: stp x1,lr,[sp,-16]! // save registers mov x12,0 // counter byte input string mov x14,0 // counter byte output string 1: ldrb w15,[x0,x12] // load byte string cbz x15,2f // zero final ? strb w15,[x1,x14] add x12,x12,1 add x14,x14,1 b 1b // loop 2: sub x14,x14,1 strb wzr,[x1,x14] // store final zero byte string 2 mov x0,x14 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* starting from a known character within the string and of m length */ /******************************************************************/ /* x0 contains the address of the input string */ /* x1 contains the address of the output string */ /* x2 contains the character */ /* x3 contains the length /* x0 returns number of characters or -1 if error */ subStringStChar: stp x1,lr,[sp,-16]! // save registers mov x16,0 // counter byte input string mov x14,0 // counter byte output string 1: ldrb w15,[x0,x16] // load byte string cbz x15,4f // zero final ? cmp x15,x2 // character find ? beq 2f // yes add x16,x16,1 // no -> increment indice b 1b // loop 2: strb w15,[x1,x14] add x16,x16,1 add x14,x14,1 cmp x14,x3 bge 3f ldrb w15,[x0,x16] // load byte string cbnz x15,2b // loop if no zero final 3: strb wzr,[x1,x14] // store final zero byte string 2 mov x0,x14 b 100f 4: strb w15,[x1,x14] mov x0,#-1 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* starting from a known substring within the string and of m length */ /******************************************************************/ /* x0 contains the address of the input string */ /* x1 contains the address of the output string */ /* x2 contains the address of string to start */ /* x3 contains the length /* x0 returns number of characters or -1 if error */ subStringStString: stp x1,lr,[sp,-16]! // save registers stp x20,x21,[sp,-16]! // save registers mov x20,x0 // save address mov x21,x1 // save address output string mov x1,x2 bl searchSubString cmp x0,-1 // not found ? beq 100f mov x16,x0 // counter byte input string mov x14,0 1: ldrb w15,[x20,x16] // load byte string strb w15,[x21,x14] cmp x15,#0 // zero final ? csel x0,x14,x0,eq beq 100f add x14,x14,1 cmp x14,x3 add x15,x16,1 csel x16,x15,x16,lt blt 1b // loop strb wzr,[x21,x14] mov x0,x14 // return indice 100: ldp x20,x21,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* search a substring in the string */ /******************************************************************/ /* x0 contains the address of the input string */ /* x1 contains the address of substring */ /* x0 returns index of substring in string or -1 if not found */ searchSubString: stp x1,lr,[sp,-16]! // save registers mov x12,0 // counter byte input string mov x13,0 // counter byte string mov x16,-1 // index found ldrb w14,[x1,x13] 1: ldrb w15,[x0,x12] // load byte string cbz x15,4f // zero final ? cmp x15,x14 // compare character beq 2f mov x16,-1 // no equals - > raz index mov x13,0 // and raz counter byte add x12,x12,1 // and increment counter byte b 1b // and loop 2: // characters equals cmp x16,-1 // first characters equals ? csel x16,x12,x16,eq // moveq x6,x2 // yes -> index begin in x6 add x13,x13,1 // increment counter substring ldrb w14,[x1,x13] // and load next byte cbz x14,3f // zero final ? yes -> end search add x12,x12,1 // else increment counter string b 1b // and loop 3: mov x0,x16 // return indice b 100f 4: mov x0,#-1 // yes returns error 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc" ================================================ FILE: string/tokenize_string.s ================================================ /* =============================== This program uses codes from Rosetta Code. See: https://rosettacode.org/wiki/Tokenize_a_string This code follows Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. =============================== */ /* ARM assembly AARCH64 Raspberry PI 3B */ /* program tokenize_string.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc" .equ NBPOSTESECLAT, 20 /*******************************************/ /* Initialized data */ /*******************************************/ .data szMessFinal: .asciz "Words are : \n" szString: .asciz "Hello,How,Are,You,Today" szMessError: .asciz "Error tokenize !!\n" szCarriageReturn: .asciz "\n" /*******************************************/ /* UnInitialized data */ /*******************************************/ .bss /*******************************************/ /* code section */ /*******************************************/ .text .global main main: ldr x0,qAdrszString // string address mov x1,',' // separator bl stTokenize cmp x0,-1 // error ? beq 99f mov x2,x0 // table address ldr x0,qAdrszMessFinal // display message bl affichageMess ldr x4,[x2] // number of areas add x2,x2,8 // first area mov x3,0 // loop counter mov x0,x2 1: // display loop ldr x0,[x2,x3, lsl 3] // address area bl affichageMess ldr x0,qAdrszCarriageReturn // display carriage return bl affichageMess add x3,x3,1 // counter + 1 cmp x3,x4 // end ? blt 1b // no -> loop b 100f 99: // display error message ldr x0,qAdrszMessError bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrszString: .quad szString //qAdrszFinalString: .quad szFinalString qAdrszMessFinal: .quad szMessFinal qAdrszMessError: .quad szMessError qAdrszCarriageReturn: .quad szCarriageReturn /*******************************************************************/ /* Separate string by separator into an array */ /* areas are store on the heap Linux */ /*******************************************************************/ /* x0 contains string address */ /* x1 contains separator character (, or . or : ) */ /* x0 returns table address with first item = number areas */ /* and other items contains pointer of each string */ stTokenize: stp x1,lr,[sp,-16]! // save registers mov x16,x0 mov x9,x1 // save separator mov x14,0 1: // compute length string for place reservation on the heap ldrb w12,[x0,x14] cbz x12, 2f add x14,x14,1 b 1b 2: ldr x12,qTailleTable add x15,x12,x14 and x15,x15,0xFFFFFFFFFFFFFFF0 add x15,x15,16 // align word on the heap // place reservation on the heap mov x0,0 // heap address mov x8,BRK // call system linux 'brk' svc 0 // call system cmp x0,-1 // error call system beq 100f mov x14,x0 // save address heap begin = begin array add x0,x0,x15 // reserve x15 byte on the heap mov x8,BRK // call system linux 'brk' svc 0 cmp x0,-1 beq 100f // string copy on the heap add x13,x14,x12 // behind the array mov x0,x16 mov x1,x13 3: // loop copy string ldrb w12,[x0],1 // read one byte and increment pointer one byte strb w12,[x1],1 // store one byte and increment pointer one byte cbnz x12,3b // end of string ? no -> loop mov x0,#0 str x0,[x14] str x13,[x14,8] mov x12,#1 // areas counter 4: // loop load string character ldrb w0,[x13] cbz x0,5f // end string cmp x0,x9 // separator ? cinc x13,x13,ne // no -> next location bne 4b // and loop strb wzr,[x13] // store zero final of string add x13,x13,1 // next character add x12,x12,1 // areas counter + 1 str x13,[x14,x12, lsl #3] // store address area in the table at index x2 b 4b // and loop 5: str x12,[x14] // store number areas mov x0,x14 // returns array address 100: ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 qTailleTable: .quad 8 * NBPOSTESECLAT /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc"