Linux-SWAT
Forum Addict!
- Joined
- Feb 13, 2010
- Messages
- 9,188
How should I do ?
--EDIT : ok I moved that out !
--EDIT : ok I moved that out !
Last edited by a moderator:
.text
.align 2
.macro syswrite
mov r0, #1 @ file descriptor 1 - stdout
svc 0 @ call the Linux kernel and print the text
ldr r1, =newline @ buffer to write, a newline
mov r2, #2 @ buffer length
mov r0, #1 @ file descriptor 1 - stdout
svc 0 @ call the Linux kernel and print the newline
.endm
.global _start
_start:
mov r7, #4 @ system call number, 4 is 'write'
mov r5, #5 @ set the counter to 5
_loop:
ldr r1, =datacounter
mov r2, #1 @ buffer length
syswrite
ldr r1, =datacounter
ldr r2, [r1]
sub r2, r2, #1
str r2, [r1]
ldr r1, =text @ buffer to write
mov r2, #11 @ buffer length
syswrite
subs r5, #1 @ decrement the counter
bne _loop @ if not zero, come back to _loop
mov r7, #1 @ exit
svc 0
.data
text: .asciz "hello world!"
newline: .asciz "\n"
datacounter: .word 53 @ ascii code for the number 5
Edit2: To correct myself, it does need writing back, as that syswrite call uses a buffer in memory indexed by address, and doesn't write out a byte stored in a register. I'd still be tempted to compute it by adding 48 to the counter, but only now because it saves you encoding the same data two ways.
Edit: Ah, interesting, I didn't know the system marked the data section as readonly. Presumably that's an MMU hack that stops it being written to.
At least a partial explanation - 48 is the numeric code of the character zero in ascii (49 = '1', ..., 57 = '9'). So if your counter has a value 0-9, by adding 48 you convert it to the ascii representation of the number, which you can print.Edit2: To correct myself, it does need writing back, as that syswrite call uses a buffer in memory indexed by address, and doesn't write out a byte stored in a register. I'd still be tempted to compute it by adding 48 to the counter, but only now because it saves you encoding the same data two ways.
I don't understand this _at all_ :/
Also, what is a null terminated byte ?
.include "macro.mac"
.globl _start
.data
_start:
print "hello\n"
mov r4,#5
1:
adr r1,toprintout
mov r0,#console
mov r2,#2
invoke sys_write
ldrb r0,toprintout
inc r0
strb r0,toprintout
regloop r4,1b
exit 0
toprintout: .ascii "0\n"
.align
.bss
.end
you can _start your code in the .data section. here is an example:
Code section is (by default) readable and executable, but not writable. Data section is (by default) readable and writable, but not executable.
$ ./hello
9
8
7
6
5
4
3
2
1
0
And I assume modern ARM chips have a integer divide instruction, but I've not looked into that, and I don't know how you get the modulo out of them, which is important to this.
mov r3, #10
mls r3, r2, r3, r0 @ r3 = r0 - ((r0/10) * 10)
0x00010090 in _loop ()
r0 0x0 0
r1 0xbefff58f 3204445583
r2 0x0 0
r3 0x80368000 2151055360
r4 0x0 0
r5 0x4000 16384
r6 0x0 0
r7 0x4 4
r8 0x0 0
r9 0x0 0
r10 0x200da 131290
r11 0x1 1
r12 0x0 0
sp 0xbefff450 0xbefff450
lr 0x0 0
pc 0x10090 0x10090 <_loop+16>
cpsr 0x20000010 536870928