flat assembler
Message board for the users of flat assembler.

Index > Main > Help with hand assembly

Author
Thread Post new topic Reply to topic
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 20 Aug 2024, 23:09
I will occasionally need some machine language generated for two or three lines of code at a time.
Right now I would like some verification that I have converted these two lines correctly.

sub [Stk],4 imm to mem 1000 00sw: mod 101 r/m: disp : imm
1000 0000: 10 101 100: Stk(32) : 0x04
mov [Stk],dword lit imm to mem 1100 011w: mod 000 r/m: disp : imm
1100 0111: 10 000 100: Stk(32): (lit32)
Any help is appreciated!
Post 20 Aug 2024, 23:09
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20335
Location: In your JS exploiting you and your system
revolution 20 Aug 2024, 23:30
Code:
~ printf 'use32 \n sub byte[0x12345678],4' > test.asm ; fasm test.asm test.bin ; hd test.bin
flat assembler  version 1.73.31  (16384 kilobytes memory)
1 passes, 7 bytes.
00000000  80 2d 78 56 34 12 04                              |.-xV4..|
00000007

~ printf 'use32 \n mov dword[0x12345678],4' > test.asm ; fasm test.asm test.bin ; hd test.bin
flat assembler  version 1.73.31  (16384 kilobytes memory)
1 passes, 10 bytes.
00000000  c7 05 78 56 34 12 04 00  00 00                    |..xV4.....|
0000000a

~ 
    
Post 20 Aug 2024, 23:30
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1620
Location: Toronto, Canada
AsmGuru62 20 Aug 2024, 23:37
FASM is made to generate opcodes for you:
Code:
Stk dd 0

00405006  |.  832D AC244000 04            SUB     DWORD PTR DS:[4024AC], 4
0040500D  |.  C705 AC244000 78921277      MOV     DWORD PTR DS:[4024AC], 77129278
    

I am not sure why to do it by hand.
If this is code to emulate a stack, you are missing one more instruction:
Code:
00405006  |.  832D AC244000 04    SUB     DWORD PTR DS:[4024AC], 4
0040500D  |.  8B0D AC244000       MOV     ECX, DWORD PTR DS:[4024AC]
00405013  |.  C701 78921277       MOV     DWORD PTR DS:[ECX], 77129278
    
Post 20 Aug 2024, 23:37
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1620
Location: Toronto, Canada
AsmGuru62 21 Aug 2024, 01:38
I see.
You maybe trying to generate the actual CPU code for compiled source.
Then you still do not need to generate by hand.
Just use FASM and debugger.
Post 21 Aug 2024, 01:38
View user's profile Send private message Send e-mail Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 21 Aug 2024, 02:44
IF fasm can do it I am all for trying to do so. It will make this topic very short. I still can't use a debugger though. How did you tell fasm to do that? Just a normal assemble and then use debugger to read it back?

Thanks for the help
The code was to put a number onto the stack. It's usage is to be embedded into a compiled word so that it runs during runtime for that compiled word. This is very common in forth. Since the source for the second command is an immediate and not considered directly from memory it shouldn't need the extra step through the register. If it was impossible fasm would have complained.
Post 21 Aug 2024, 02:44
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4039
Location: vpcmpistri
bitRAKE 21 Aug 2024, 08:52
The tool presented in this video and this thread allows viewing the resulting bytes as you edit code - "real-time assembler".

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 21 Aug 2024, 08:52
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1620
Location: Toronto, Canada
AsmGuru62 21 Aug 2024, 17:29
"Just a normal assemble and then use debugger to read it back?"
Yes.

So, you are not use a debugger because the font is too small?
I think someone posted the way to increase the font.

In any event, let us see what is happening with the two lines of code you posted:
Code:
    sub [Stk],4
    mov [Stk],0x11223344
    

I am assuming the 'Stk' is a pointer to the top of the stack and it is defined as 'DD'.

Line #1 says: "subtract 4 from the contents of the label 'Stk'"
Line #2 says: "store 0x11223344 into the contents of the label 'Stk'"

Basically, you are replacing (damaging) the stack top pointer with a value of 0x11223344.

What you need is to store 0x11223344 into where stack top pointer points to (after SUB).
That is why you need to load the 'Stk' into a register and write 0x11223344 into that address.
Code:
+--------------+-- [Stk]-4  after SUB
|  0x11223344  |
+--------------+-- [Stk]    before SUB
|              |
|              |
|              |
|              |
|              |
+--------------+
    
Post 21 Aug 2024, 17:29
View user's profile Send private message Send e-mail Reply with quote
Stancliff



Joined: 30 Jun 2024
Posts: 54
Location: Florida
Stancliff 22 Aug 2024, 02:53
registers behave differently than variables during indirection... not the first time I have been caught like that. This is a big incentive to keep the data stack in a register instead of a variable. I have to double check some code here and there since I doubt I implemented the variable based stack pointer correctly.
Post 22 Aug 2024, 02:53
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.