.text
.align 2
.macro syswrite @ print what r1 points at, must be ascii, buffer length r2 must be set
push {r0, r7}
mov r7, #4 @ system call number, 4 is 'write'
mov r0, #1 @ file descriptor 1 - stdout
svc 0 @ call the Linux kernel and print the ascii text
pop {r0, r7}
.endm
.macro newline
push {r1}
ldr r1, =newline @ point on the buffer to write, \n, a newline
mov r2, #1 @ newline buffer length
syswrite
pop {r1}
.endm
.macro zero
push {r1, r2, r11}
ldr r1, =asciicounter @ point on =asciicounter
add r11, r9, #48 @ convert the digit counter to ascii
str r11, [r1] @ store it to be displayed
mov r2, #1 @ buffer length
syswrite
pop {r1, r2, r11}
.endm
.macro one
push {r1, r2, r11}
ldr r1, =asciicounter @ point on =asciicounter
add r11, r10, #48 @ convert the digit counter to ascii
str r11, [r1] @ store it to be displayed
mov r2, #1 @ buffer length
syswrite
pop {r1, r2, r11}
.endm
.global _start
_start:
mov r5, #12 @ set the number
mov r8, #1 @ set the "cursor"
mov r9, #0 @ constant 0
mov r10, #1 @ constant 1
_loop1:
mov r8, r8, lsl #1 @ r8 * 2
cmp r8, r5 @ increases to know when the number is < x^2
ble _loop1
_loop2:
mov r8, r8, lsr #1 @ r8 / 2
cmp r8, #0
beq _end @ cursor at 0 -> end
cmp r5, r8 @ display a 0 or a 1
blt _zero
bge _one
b _loop2
_zero:
zero
b _loop2
_one:
one
sub r5, r8 @ remove the msb for the next comparison
b _loop2
_end:
newline @ the binary number is now assembled
mov r7, #1 @ exit
svc 0 @ call the Linux kernel and exit
.data
asciicounter: .word 0 @ must be initialized
newline: .ascii "\n" @ \n is a newline
div_10_reciprocal: .word 429496730 @ ceil((2^32)/10)
.text
.align 2
.macro syswrite @ print what r1 points at, must be ascii, buffer length r2 must be set
push {r0, r7}
mov r7, #4 @ system call number, 4 is 'write'
mov r0, #1 @ file descriptor 1 - stdout
svc 0 @ call the Linux kernel and print the ascii text
pop {r0, r7}
.endm
.macro newline
push {r1}
ldr r1, =newline @ point on the buffer to write, \n, a newline
mov r2, #1 @ newline buffer length
syswrite
pop {r1}
.endm
.macro zero
push {r1, r2, r11}
ldr r1, =asciicounter @ point on =asciicounter
add r11, r9, #48 @ convert the digit counter to ascii
str r11, [r1] @ store it to be displayed
mov r2, #1 @ buffer length
syswrite
pop {r1, r2, r11}
.endm
.macro one
push {r1, r2, r11}
ldr r1, =asciicounter @ point on =asciicounter
add r11, r10, #48 @ convert the digit counter to ascii
str r11, [r1] @ store it to be displayed
mov r2, #1 @ buffer length
syswrite
pop {r1, r2, r11}
.endm
.global _start
_start:
mov r5, #12 @ set the number
mov r8, #1 @ set the "cursor"
mov r9, #0 @ constant 0
mov r10, #1 @ constant 1
_loop1:
mov r8, r8, lsl #1 @ r8 * 2
cmp r8, r5 @ increases to know when the number is < x^2
ble _loop1
_loop2:
movs r8, r8, lsr #1 @ r8 / 2
beq _end @ cursor at 0 -> end
cmp r5, r8 @ display a 0 or a 1
blt _zero
bge _one
_zero:
zero
b _loop2
_one:
one
sub r5, r8 @ remove the msb for the next comparison
b _loop2
_end:
newline @ the binary number is now assembled
mov r7, #1 @ exit
svc 0 @ call the Linux kernel and exit
.data
asciicounter: .word 0 @ must be initialized
newline: .ascii "\n" @ \n is a newline
div_10_reciprocal: .word 429496730 @ ceil((2^32)/10)
Out of interest, did you discover the mistake you were making before that made it print out the wrong result?
cmp r5, r8 @ display a 0 or a 1
movlt r9, #0
movge r9, #1
subge r5, r8 @ remove the msb for the next comparison
zero @ print the value of r9
b _loop2
.text
.align 2
.macro syswrite @ print what r1 points at, must be ascii, buffer length r2 must be set
mov r7, #4 @ system call number, 4 is 'write'
mov r0, #1 @ file descriptor 1 - stdout
svc 0 @ call the Linux kernel and print the ascii text
.endm
.macro newline
ldr r1, =newline @ point on the buffer to write, \n, a newline
mov r2, #1 @ newline buffer length
syswrite
.endm
.macro zero
ldr r1, =asciizero @ point on ascii 0
mov r2, #1 @ buffer length
syswrite
.endm
.macro one
ldr r1, =asciione @ point on ascii 1
mov r2, #1 @ buffer length
syswrite
.endm
.global _start
_start:
mov r5, #12 @ set the number
mov r8, #1 @ set the "cursor"
_getLength:
mov r8, r8, lsl #1 @ r8 * 2
cmp r8, r5 @ increases to know when the number is < x^2
ble _getLength
_display:
movs r8, r8, lsr #1 @ r8 / 2
beq _end @ cursor at 0 -> end
cmp r5, r8 @ display a 0 or a 1
bge _one
_zero:
zero
b _display
_one:
one
sub r5, r8 @ remove the msb for the next comparison
b _display
_end:
newline @ the binary number is now assembled
mov r7, #1 @ exit
svc 0 @ call the Linux kernel and exit
.data
asciicounter: .word 0 @ must be initialized
asciizero: .ascii "0"
asciione: .ascii "1"
newline: .ascii "\n" @ \n is a newline
div_10_reciprocal: .word 429496730 @ ceil((2^32)/10)
.text
.align 2
.macro syswritechar @ print what r1 points at, must be ascii
push {r0, r2}
mov r2, #1 @ buffer length
mov r7, #4 @ system call number, 4 is 'write'
mov r0, #1 @ file descriptor 1 - stdout
svc 0 @ call the Linux kernel and print the ascii text
pop {r0, r2}
.endm
.macro newline
push {r1, r2}
ldr r1, =newline @ point on the buffer to write, \n, a newline
syswritechar
pop {r1, r2}
.endm
.global _start
_start:
ldr r1, =asciicounter @ point on =asciicounter
ldr r2, =511 @ load the value to be displayed
mov r3, #15 @ load a 0b1111 mask to isolate hex segments
mov r4, #8 @ number of rotations
_loop:
ror r2, #28 @ rotate left the value
and r5, r2, r3 @ and "mask" on the higher hex segment, and so on
cmp r5, #9 @ 0-9
add r5, #48 @ then "ascii" it
ble _num @ and go for display
add r5, #7 @ or A-F and increase the ascii number
_num:
str r5, [r1] @ display it
syswritechar
subs r4, #1 @ decrease the rotation counter
bne _loop
_end:
newline @ the binary number is now assembled
mov r7, #1 @ exit
svc 0 @ call the Linux kernel and exit
.data
asciicounter: .word 0 @ must be initialized
newline: .ascii "\n" @ \n is a newline
blt _num @ and go for display
ble _num @ and go for display
.text
.align 2
.macro syswritechar @ print what r1 points at, must be ascii
mov r7, #4 @ system call number, 4 is 'write'
mov r2, #1 @ buffer length
mov r0, #1 @ file descriptor 1 - stdout
svc 0 @ call the Linux kernel and print the ascii char
.endm
.macro newline
ldr r1, =newline @ point on the buffer to write, \n, a newline
syswritechar
.endm
.global _start
_start:
ldr r3, =511 @ set the number
mov r4, #1 @ load a 0b1 mask to isolate bit segments
mov r5, #32 @ number of rotations
_loop:
ror r3, #31 @ rotate left the value
ands r1, r3, r4 @ and "mask" on the higher bit segment, and so on
ldreq r1, =asciizero
ldrne r1, =asciione
syswritechar
subs r5, #1 @ decrease the rotation counter
bne _loop
_end:
newline @ the binary number is now assembled
mov r7, #1 @ exit
svc 0 @ call the Linux kernel and exit
.data
asciizero: .ascii "0"
asciione: .ascii "1"
newline: .ascii "\n" @ \n is a newline