Skip to content

Instantly share code, notes, and snippets.

@rixtox
Last active October 27, 2016 15:07
Show Gist options
  • Save rixtox/96e5452dfa49c3df5b0b69053458dd62 to your computer and use it in GitHub Desktop.
Save rixtox/96e5452dfa49c3df5b0b69053458dd62 to your computer and use it in GitHub Desktop.
.data
scanf_fmt:
.asciz "%d"
printf_fmt:
.asciz "%d\n"
.text
.global main
main:
push {fp, lr} // Push the caller's register states
mov fp, sp // Save stack pointer to a register (frame pointer reg)
sub sp, sp, #8 // Create 8 bytes for 2 int values on the stack (stack
// grows from the end, thus substract)
ldr r0, =scanf_fmt // Put the string address on r0 (1st argument to scanf)
add r1, sp, #4 // Put the address of the first local variable to r1
// (2nd argument to scanf)
bl scanf // scanf("%d", &a);
ldr r0, =scanf_fmt // Since scanf overwrites r0, we re-assign it
mov r1, sp // Put the address of the second local variable to r1
bl scanf // scanf("%d", &b);
ldr r1, [sp, #4] // Put the value of the first local variable to r1
ldr r2, [sp] // Put the value of the second local variable to r2
cmp r1, r2 // Compare r1 and r2
bge .print // If r1 >= r2, go print r1
mov r1, r2 // Otherwise, first set r1 = r2
.print:
ldr r0, =printf_fmt // Put the string address on r0
bl printf // Since r1 is already the value we want to print,
// printf("%d\n", r1);
mov r0, #0 // return 0;
mov sp, fp // Restore stack pointer from frame pointer reg
pop {fp, pc} // Pop the caller's register states
.data
printf_fmt:
.asciz "%d\n"
.text
.global main
main:
push {fp, lr} // Push the caller's register states
mov fp, sp // Save stack pointer to a register (frame pointer reg)
sub sp, sp, #104 // 13*8=104 bytes for string buffer
// (8-byte alignment for stack pointer is required)
mov r0, sp // Put the buffer start address on r0
mov r1, #101 // Set the size limit to 101 bytes (null-terminated)
ldr r2, =stdin // Get the address of stdin global variable
ldr r2, [r2] // Get the content of stdin global variable
bl fgets // fgets(buf,101,stdin);
mov r1, sp // r1 = &buf[0];
b .compare // Start the compare loop
.next:
add r1, r1, #1 // r1++, loop for the next char
.compare:
ldrb r0, [r1] // Read the current char
cmp r0, #0 // If it's the terminate char
beq .print // Stop and print the result
cmp r0, #10 // Or if it's not the new line
bne .next // loop for the next char
.print:
ldr r0, =printf_fmt // Put the string address in r0
sub r1, r1, sp // Calculate the looped string length
bl printf // Print the result
mov r0, #0 // return 0;
mov sp, fp // Restore stack pointer from frame pointer reg
pop {fp, pc} // Pop the caller's register states
.data
scanf_fmt:
.asciz "%d"
printf_fmt:
.asciz "%d\n"
.text
.global main
main:
push {fp, lr} // Push the caller's register states
mov fp, sp // Save stack pointer to a register (frame pointer reg)
sub sp, sp, #4 // 4 bytes for scanf int read
mov r4, #0 // sum
mov r5, #5 // count
.next:
ldr r0, =scanf_fmt
mov r1, sp
bl scanf
ldr r0, [sp]
add r4, r4, r0 // Sum the read int
sub r5, r5, #1 // Decrease count
cmp r5, #0 // If count down to 0, print
bgt .next
ldr r0, =printf_fmt
mov r1, r4
bl printf
mov r0, #0
mov sp, fp
pop {fp, pc}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment