Return to “Everything & Anything”

Post

Questions about Assembly

#1
I figured that I'd make a thread about this, because I'm bored, and I like to avoid StackOverflow. :ghost:

Does anybody see the difference between this code:

Code: Select all

section .data
p db 'helloworld.txt', 0
msg db 'hello world', 0
len equ $ - msg
newline db 10
section .bss
msg1: resb 64
section .text
global _start
_start:

        call openfile
        mov rdi, rax
        mov rax, 1
        mov rsi, msg
        mov rdx, len
        syscall
        call openfile
        mov rdi, rax
        mov rax, 1
        mov rsi, newline
        syscall
        call openfile
        mov rdi, rax
        mov rax, 0
        mov rsi, msg1
        mov rdx, 12
        syscall
        mov rax, 3
        mov rdi, p
        syscall
        mov rax, 1
        mov rdi, 1
        mov rsi, msg1
        mov rdx, 64
        syscall

mov rax, 60
syscall

openfile:
        mov rax, 2
        mov rdi, p
        mov rsi, 2
        ret
And this code:

Code: Select all

section .data
p db 'helloworld.txt', 0
msg db 'hello world', 0
len equ $ - msg
newline db 10
section .bss
msg1: resb 64
section .text
global _start
_start:

        mov rax, 2
        mov rdi, p
        mov rsi, 2
        syscall
        mov rdi, rax
        mov rax, 1
        mov rsi, msg
        mov rdx, len
        syscall
        mov rax, 2
        mov rdi, p
        mov rsi, 2
        syscall
        mov rdi, rax
        mov rax, 1
        mov rsi, newline
        syscall
        mov rax, 2
        mov rdi, p
        mov rsi, 2
        syscall
        mov rdi, rax
        mov rax, 0
        mov rsi, msg1
        mov rdx, 12
        syscall
        mov rax, 3
        mov rdi, p
        syscall
        mov rax, 1
        mov rdi, 1
        mov rsi, msg1
        mov rdx, 64
        syscall

mov rax, 60
syscall
Besides the obvious, I mean. :ghost:
Last edited by Idunno on Sun Jun 21, 2020 1:32 pm, edited 1 time in total.
Image The results of logic, of natural progression? Boring! An expected result? Dull! An obvious next step? Pfui! Where is the fun in that? A dream may soothe, but our nightmares make us run!
Post

Re: Questions about Assembly

#6
0111narwhalz wrote:
Sun Jun 21, 2020 12:08 pm
May I suggest using code tags? It's hard enough to read, no reason to make it harder by use of a proportional font.
There are code tags? :wtf:

Hello Narwhalz. :wave:
Cornflakes_91 wrote:
Sun Jun 21, 2020 12:52 pm
Idunno wrote:
Sun Jun 21, 2020 9:45 am
Cornflakes_91 wrote:
Sun Jun 21, 2020 9:43 am
what does "syscall" do?
It's int 0x80 but for 64-bit assembly. :shh:
that is actually less than helpful :P

the last time i handled assembly was some 10 years ago on some ZX80 training board
:P
To use an example:

Code: Select all

mov rax, 1
mov rdi, 1
mov rsi, msg1
mov rdx, 64
syscall
What I'm doing here is loading the RAX register with the output system call, loading the RDI register with the output system call, then loading RSI with the string output, loading RDX with the length of the string output. But, all of this has to execute at once, so, once it's all loaded, it needs to be sent through. That's what int 0x80 does, it sends the instructions to the OS to be executed. Otherwise nothing happens. :ghost:
Last edited by Idunno on Mon Jun 22, 2020 11:18 am, edited 1 time in total.
Image The results of logic, of natural progression? Boring! An expected result? Dull! An obvious next step? Pfui! Where is the fun in that? A dream may soothe, but our nightmares make us run!
Post

Re: Questions about Assembly

#7
Does anybody know how to use macros? 'Cause I don't. :ghost:

Furthest I've gotten:

Code: Select all

%macro openfile 0
        mov rax, 2
        mov rdi, p
        mov rsi, 2
        syscall
%endmacro

section .data
p db 'helloworld.txt', 0
msg db 'hello world', 0
len equ $ - msg
newline db 10
section .bss
msg1: resb 64
section .text
global _start
_start:

        openfile 
        mov rdi, rax
        mov rax, 1
        mov rsi, msg
        mov rdx, len
        syscall
        openfile 
        mov rdi, rax
        mov rax, 1
        mov rsi, newline
        syscall
        openfile 
        mov rdi, rax
        mov rax, 0
        mov rsi, msg1
        mov rdx, 12
        syscall
        mov rax, 3
        mov rdi, p
        syscall
        mov rax, 1
        mov rdi, 1
        mov rsi, msg1
        mov rdx, 64
        syscall

mov rax, 60
syscall
Image The results of logic, of natural progression? Boring! An expected result? Dull! An obvious next step? Pfui! Where is the fun in that? A dream may soothe, but our nightmares make us run!
Post

Re: Questions about Assembly

#8
Is there a reason that opening a file to add stuff to it later causes the stuff to be added to the front of the file, as opposed to the back of the file? Or why it replaces characters that were already there? :eh:
Image The results of logic, of natural progression? Boring! An expected result? Dull! An obvious next step? Pfui! Where is the fun in that? A dream may soothe, but our nightmares make us run!
Post

Re: Questions about Assembly

#9
Why does this work:

Code: Select all

openfile p
        mov rdi, rax
        mov rax, 1
        mov rsi, msg2
        mov rdx, len2
        syscall
While this:

Code: Select all

openfile p
        mov rax, 1
        mov rdi, p
        mov rsi, msg
        mov rdx, len
        syscall
Does not? :think:
Image The results of logic, of natural progression? Boring! An expected result? Dull! An obvious next step? Pfui! Where is the fun in that? A dream may soothe, but our nightmares make us run!
Post

Re: Questions about Assembly

#11
Idunno wrote:
Mon Jun 22, 2020 7:56 pm
Are the file descriptor and the file name two different things? :think:
Yes.
Though I know nothing about ASM.

A file name is simply the human readable method of finding a specific item through the file system.

A file descriptor is local reference to a file. An FD is created when you 'open' a file with an application.

Likely you have not been storing the file descriptor in the past. The FD needs to be handed to all file manipulation work after you open it.
°˖◝(ಠ‸ಠ)◜˖°
WebGL Spaceships and Trails
<Cuisinart8> apparently without the demon driving him around Silver has the intelligence of a botched lobotomy patient ~ Mar 04 2020
console.log(`What's all ${this} ${Date.now()}`);
Post

Re: Questions about Assembly

#12
Silverware wrote:
Mon Jun 22, 2020 9:33 pm
Idunno wrote:
Mon Jun 22, 2020 7:56 pm
Are the file descriptor and the file name two different things? :think:
Yes.
Though I know nothing about ASM.

A file name is simply the human readable method of finding a specific item through the file system.

A file descriptor is local reference to a file. An FD is created when you 'open' a file with an application.

Likely you have not been storing the file descriptor in the past. The FD needs to be handed to all file manipulation work after you open it.
Thanks Silverware. Also, hi Silverware. :wave:

Something tells me that half of my problems are due to my lack of knowledge of Linux file systems, as opposed to any inability to program in assembly. :ghost:
Image The results of logic, of natural progression? Boring! An expected result? Dull! An obvious next step? Pfui! Where is the fun in that? A dream may soothe, but our nightmares make us run!
Post

Re: Questions about Assembly

#13
Linux Filesystems should be completely irrelevant to you.
As these systems are the same on any OS.

You open a File, get presented a File Descriptor.

You then need to pass the FD to any file operation you intend to do.
You also need to move the seek header on the file back and forward. As it reads or writes from this point.
Most read operations will move it, as will most writes.

When you open in R or RW it'll be at the 0 byte, you need to open with the A flag to get the Nth Byte, as thats the append flag.
*OR* move the seek header.

I have no idea how to do that in ASM, but most low level file tools need to do this.


In PHP this looks something like:

Code: Select all

$fileHandle = fopen('/path/to/file','a'); // Opes a file in Write mode and moves the cursor to the end of the file, returns a fileHeader, or FALSE
fwrite($fileHandle,"some content",12); // Writes 12 bytes, "some content" to the file opened above. Returns 12 (bytes written) or FALSE if failed.
fseek($fileHandle,0,SEEK_SET); // Moves the seek header to the 0th Byte in the file.
fwrite($fileHandle,"1",1); // Writes the value "1" to the first byte on the file.
fclose($fileHandle); // Close the file handle, not always strictly required, as the end of the app *SHOULD* auto-close all files at the OS level.
°˖◝(ಠ‸ಠ)◜˖°
WebGL Spaceships and Trails
<Cuisinart8> apparently without the demon driving him around Silver has the intelligence of a botched lobotomy patient ~ Mar 04 2020
console.log(`What's all ${this} ${Date.now()}`);
Post

Re: Questions about Assembly

#14
Silverware wrote:
Tue Jun 23, 2020 3:19 pm
Linux Filesystems should be completely irrelevant to you.
As these systems are the same on any OS.

You open a File, get presented a File Descriptor.

You then need to pass the FD to any file operation you intend to do.
You also need to move the seek header on the file back and forward. As it reads or writes from this point.
Most read operations will move it, as will most writes.

When you open in R or RW it'll be at the 0 byte, you need to open with the A flag to get the Nth Byte, as thats the append flag.
*OR* move the seek header.

I have no idea how to do that in ASM, but most low level file tools need to do this.


In PHP this looks something like:

Code: Select all

$fileHandle = fopen('/path/to/file','a'); // Opes a file in Write mode and moves the cursor to the end of the file, returns a fileHeader, or FALSE
fwrite($fileHandle,"some content",12); // Writes 12 bytes, "some content" to the file opened above. Returns 12 (bytes written) or FALSE if failed.
fseek($fileHandle,0,SEEK_SET); // Moves the seek header to the 0th Byte in the file.
fwrite($fileHandle,"1",1); // Writes the value "1" to the first byte on the file.
fclose($fileHandle); // Close the file handle, not always strictly required, as the end of the app *SHOULD* auto-close all files at the OS level.
I have no idea how to do that in ASM, but most low level file tools need to do this.
most low level file tools need to do this.
low level file tool
I think I may have found the problem, I don't use those. :ghost:

Figured out how to do it in assembly tho. The file descriptor is left in the register that opened the file in the first place, it needs to extracted to a variable, where it can then be used freely. Which is something I should have figured out from the numbers. :?
Image The results of logic, of natural progression? Boring! An expected result? Dull! An obvious next step? Pfui! Where is the fun in that? A dream may soothe, but our nightmares make us run!
Post

Re: Questions about Assembly

#15
Idunno wrote:
Tue Jun 23, 2020 5:03 pm
I have no idea how to do that in ASM, but most low level file tools need to do this.
most low level file tools need to do this.
low level file tool
I think I may have found the problem, I don't use those. :ghost:

Figured out how to do it in assembly tho. The file descriptor is left in the register that opened the file in the first place, it needs to extracted to a variable, where it can then be used freely. Which is something I should have figured out from the numbers. :?
Your tool is lower level, that's all.
You have to run a few more operations on anything. But the same basic rules apply.

Open a file with W, or RW, or A flags to write.
Move the cursor to wherever you want to write to (this is why A is great, it STARTS at the end)
Then write data.
Then either repeat, or close the file descriptor.

But yeah, you *GOTTA* pay attention to the File Descriptor.

You also wanna confirm Actual bytes written to intended bytes written per step.
Write a function/macro/library or whatever to handle files. it'll pay off in the short run, let alone the long run.
°˖◝(ಠ‸ಠ)◜˖°
WebGL Spaceships and Trails
<Cuisinart8> apparently without the demon driving him around Silver has the intelligence of a botched lobotomy patient ~ Mar 04 2020
console.log(`What's all ${this} ${Date.now()}`);

Online Now

Users browsing this forum: No registered users and 24 guests

cron