@define my Raspberry Pi .cpu cortex-a53 .fpu neon-fp-armv8 @program code .data print1: .asciz "Dice rolls: " out1: .asciz "%d " print2: .asciz "\nNumber of runs: %d\n" array: .space 80 @ array of 20 words .text .align 2 .global main .type main, %function @program code main: push {lr} @ save return address @ seed random generator using current time mov r0, #0 @ pass zero (NULL) to time bl time @ get current time bl srand @ initialize seed with current time @ fill the dice array ldr r0, =array @ load address of array bl roll_dice @ roll dice and fill array @ print message before printing array ldr r0, =print1 @ load address of message bl printf @ print string @ print the dice array ldr r0, =array @ load address of array bl print_rolls @ print dice rolls @ print the number of runs ldr r0, =array @ load address of array bl count_run @ count and print number of runs pop {lr} @ restore return address bx lr @ return and exit program @ Function to generate 20 dice rolls @ roll_dice(int arr[20]) .global roll_dice .type roll_dice, %function roll_dice: push {r4-r6, lr} @ save registers mov r4, r0 @ copy array address to r4 mov r5, #20 @ initialize counter to 20 mov r6, #6 @ load 6 for making divisions roll_loop: bl rand @ generate random number udiv r1, r0, r6 @ divide by 6 mul r2, r1, r6 @ calculate remainder, multiply quotient by 6 sub r0, r0, r2 @ subtract multiplication from initial number to get remainder str r0, [r4] @ save random number in array add r4, r4, #4 @ advance array pointer subs r5, r5, #1 @ decrement count bne roll_loop @ continue while not zero pop {r4-r6, lr} @ restore registers bx lr @ Function to print the 20 dice rolls @ print_rolls(int arr[20]) .global print_rolls .type print_rolls, %function print_rolls: push {r4-r5, lr} @ save registers mov r4, r0 @ copy array address to r4 mov r5, #20 @ initialize counter to 20 print_loop: ldr r0, =out1 @ load format to print an integer with a space ldr r1, [r4] @ load value from array bl printf @ generate random number add r4, r4, #4 @ advance array pointer subs r5, r5, #1 @ decrement count bne print_loop @ continue while not zero pop {r4-r5, lr} @ restore registers bx lr @ Function to count the number of runs in the 20 dice rolls @ count_run(int arr[20]) .global count_run .type count_run, %function count_run: push {r4-r7, lr} @ save registers mov r4, #20 @ initialize counter to 20 mov r5, #-1 @ initialize previous value to -1 mov r6, #0 @ initialize number of repetitions to 0 mov r7, #0 @ initialize number of runs to 0 count_loop: ldr r1, [r0] @ load number from array cmp r1, r5 @ compare with previous value beq next @ if repeated, go to next cmp r6, #2 @ see if there was at least two values in run addge r7, r7, #1 @ if so, increment number of runs mov r6, #0 @ restart repetitions to 0 next: add r6, r6, #1 @ increment count of value repetition mov r5, r1 @ set current value as the previous add r0, r0, #4 @ advance array pointer subs r4, r4, #1 @ decrement count bne count_loop @ continue while not zero @ print number of runs ldr r0, =print2 @ load address of message mov r1, r7 @ load number of runs bl printf @ print string pop {r4-r7, lr} @ restore registers bx lr
Write, Run & Share Assembly code online using OneCompiler's Assembly online compiler for free. It's one of the robust, feature-rich online compilers for Assembly language. Getting started with the OneCompiler's Assembly compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Assembly
and start coding.
Assembly language(asm) is a low-level programming language, where the language instructions will be more similar to machine code instructions.
Every assembler may have it's own assembly language designed for a specific computers or an operating system.
Assembly language requires less execution time and memory. It is more helful for direct hardware manipulation, real-time critical applications. It is used in device drivers, low-level embedded systems etc.
Assembly language usually consists of three sections,
Data section
To initialize variables and constants, buffer size these values doesn't change at runtime.
bss section
To declare variables
text section
_start
specifies the starting of this section where the actually code is written.
There are various define directives to allocate space for variables for both initialized and uninitialized data.
variable-name define-directive initial-value
Define Directive | Description | Allocated Space |
---|---|---|
DB | Define Byte | 1 byte |
DW | Define Word | 2 bytes |
DD | Define Doubleword | 4 bytes |
DQ | Define Quadword | 8 bytes |
DT | Define Ten Bytes | 10 bytes |
Define Directive | Description |
---|---|
RESB | Reserve a Byte |
RESW | Reserve a Word |
RESD | Reserve a Doubleword |
RESQ | Reserve a Quadword |
REST | Reserve a Ten Bytes |
Constants can be defined using
CONSTANT_NAME EQU regular-exp or value
%assign constant_name value
%define constant_name value
Loops are used to iterate a set of statements for a specific number of times.
mov ECX,n
L1:
;<loop body>
loop L1
where n specifies the no of times loops should iterate.
Procedure is a sub-routine which contains set of statements. Usually procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
procedure_name:
;procedure body
ret