Reputation: 1673
I am learning assembly and write the following:
Section .text
global _start
_start:
jmp short GoToFilename
open:
pop esi ; esi gets address of the filename
xor eax, eax ; clear eax
mov [esi+13], al ; terminate file name(see # at the end of the first db)
mov dl, 0xa ; dl gets code of newline(\n)
mov byte [esi+15], dl ; place it between A and # (see 2nd db)
mov [esi+16], al ; place NULL for # (at the 2nd db) gets
lea edi, [esi+14] ; edi gets address of input text (here it should be only A as input)
mov [esi+17], edi ; place its address for XXXX
mov dx, 0x1b6 ; permissions
mov cl, 0x42 ; flags
mov ebx, esi ; address of file name
mov al, 0x5 ; syscall of open
int 0x80 ; go, lets do it
mov edi, eax ; put handle to file in edi
xor eax, eax ; clear because we will need it
write:
xor edx, edx
xor ecx, ecx
xor ebx, ebx
mov dl, 0x1 ; number of bytes to write = 1
lea ecx, [esi+17] ; ecx gets address of input text
mov ebx, edi ; put handle to file in edi
mov al, 0x4 ; syscall of write
int 0x80 ; go, lets do it
close:
mov ebx, edi ; handle to file
mov al, 0x6 ; syscall of open
int 0x80 ; go, lets do it
exit:
xor ebx, ebx ; clear ebx
mov al, 0x1 ; syscall of exit
int 0x80 ; go, lets do it
GoToFilename:
call open
db '/tmp/file.txt#'
db 'A #XXXX'
[For that, I use the jmp-call-pop-technique. Those who know what shellcodes are will it know what I mean, but if not then its not so important here]
So, when I let it run, then the file is created but when I open the file then I get the sign # written in file, and not the character 'A'.
Do you know where I made the mistake ? I couldnt find it. I check the offsets, go through the code many times...but without success.
best regards,
Upvotes: 0
Views: 80
Reputation: 39691
You are wrongly reffering to the address stored at [esi+17]. Better use:
lea ecx, [esi+14] ; ecx gets address of input text
Alternatively
mov ecx, [esi+17]
Upvotes: 1