flat assembler
Message board for the users of flat assembler.
![]() Goto page Previous 1, 2 |
Author |
|
fasmnewbie 25 Feb 2014, 17:38
AsmGuru62 wrote: One day I will write a manual on OllyDbg with images and stuff, so every new coder will be able to debug their code. That would be nice Guru. This is the kind of help to newbies that we should promote on this board. When I first came into contact with FASM, I was so surprised to see that nobody's is offering students/newbies anything. Is FASM intended for seasoned and elite programmers only? The first tutorial I saw was from "Jakash" or something. Then there's vid's short tutorial and then FASM's docs and then this board and then you are on your own. That's where my motivation came from (only two years later when I started FASM as a serious hobby). I started with prtc macro, then prtbin and then the prtr macros just to help my self understand how all these things work. And then I thought, why not share these macros with others so that they don't have PITA while learning FASM. I don't blame Privalov for this really. He's done a great job providing us a nice assembler and I think he's contributing enough. No offense to privalov. |
|||
![]() |
|
l_inc 25 Feb 2014, 21:16
fasmnewbie
Code: ;Lesson 1.2 ROTATE LEFT. Revisited. org 100h include 'macro\proc16.inc' postpone { include 'lib16.inc' } stdcall pbin,[x] ;Prints x in binary form... rol [x],1 ;Can you really rotate [x] by 1 bit left?? stdcall line stdcall pbin,[x] ;oh, you just did!! Seee!! You learned that fast, huh?? stdcall pquit ;— wait, what is stdcall? ;— It's a macro. Just use it, otherwise IT WON'T WORK! x dw 30 _________________ Faith is a superposition of knowledge and fallacy |
|||
![]() |
|
fasmnewbie 26 Feb 2014, 02:21
l_inc wrote: fasmnewbie Hahaha. Man, that's funny. I knew it that you'd go there . hahaha ![]() Maybe next time with "proc16". I wasn't aware of this undocumented feature until just recently. But hey, If newbies can learn fast by using the macro set, I will be very happy, provided that they use it correctly to probe and test their code. They can even use the macro to "debug" on-the-fly while learning about procs. Because in the end, they will not be using any of it once they have completed their code. It serves its purpose well as a tool. And that's a nice thing about that macro set. No extra overhead - not even a single byte committed to their main program after completion. Since I am thinking of updating the macros soon, maybe you can contribute a macro or two to the existing set. I am still missing random number utility. Math is really not my thing. I really appreciate your help (again). Thanks. |
|||
![]() |
|
l_inc 26 Feb 2014, 13:28
fasmnewbie
Quote: I wasn't aware of this undocumented feature until just recently I knew, there must be something missing in your argumentation in favor of macros. More revelations to come ![]() Quote: No extra overhead - not even a single byte committed to their main program after completion I'd like to present one more astonishing fact about procedures (proc macro). Procedures also do not add any single byte to the program, unless you explicitly call them. This is particularly documented even in the wiki. Quote: maybe you can contribute a macro or two to the existing set I'm more on writing very general purpose macros, that produce very little code if any at all. And those should not add an extensive syntax with lots of peculiarities. Once you start forcing people to narrow their code down to your specific abstractions not consistent with their own coding style or macro sets, they will refuse to make use of your work. Therefore I prefer to write something "useful for anyone". Like displaying binary contents: Code: match , { local begin_s, base_ns, mload_s, base_s, awidth_ns, np_s macro dispmem_ base, awidth=8, np='.' ;display base, address width, nonprintable characters \{ \local begin,base_n,awidth_n,mload,buf,omit define begin_s begin define base_ns base_n define awidth_ns awidth_n define mload_s mload define base_s base define np_s np begin = $ match any, base \\{ base_n = base \\} awidth_n = awidth macro mload \\{\\} macro org [arg] \\{ \\common match =omit,omit \\\{ \\\local ..space, sbegin, send sbegin = begin send = $ ..space:: org arg begin = $ macro mload \\\\{ mload purge mload match , base \\\\\{ base_n = sbegin \\\\\} dq base_n,send-sbegin repeat send-sbegin load buf byte from ..space:sbegin+(%-1) db buf end repeat match any, base \\\\\{ base_n = base_n+(send-sbegin) \\\\\} \\\\} \\\} match +,omit \\\{ org arg \\\} \\} macro virtual [arg] \\{ common virtual arg define omit + \\} struc virtual [arg] \\{ common . virtual arg match =end,. \\\{ restore omit \\\} \\} macro dispmem_ [arg] \\{ common display 'Error: _dispmem missing',13,10 err \\} \} macro _dispmem \{ \local dig,buf,curptr,displen,b,e,..space,ssize,spaceptr match =np_s,np_s \\{ display "Error: dispmem_ missing",13,10 err \\} ssize = $-begin_s ..space:: virtual at 0 match mload,mload_s \\{ mload purge mload \\} match , base_s \\{ base_ns = begin_s \\} dq base_ns, ssize repeat ssize load buf byte from ..space:begin_s+(%-1) db buf end repeat spaceptr = $$ while spaceptr < $ load b qword from spaceptr load e qword from spaceptr+8 if b >= 1 shl 63 b = b - 1 shl 63 - 1 shl 63 end if e = b+e spaceptr = spaceptr+16 if e-b > 0 times awidth_ns display ' ' display ' 0 1 2 3 4 5 6 7 8 9 A B C D E F',13,10 curptr = b and not $F displen = ((e+$F) and not $F - curptr) shr 4 repeat displen ;display address repeat awidth_ns dig = curptr shr ((awidth_ns-%)*4) and 0Fh+'0' if dig > '9' dig = dig-'0'+('A'-10) end if display dig end repeat display ': ' ;display hex data repeat $10 if curptr+(%-1) < b | e <= curptr+(%-1) display ' ' else load buf byte from spaceptr+(%-1)+(curptr-b) repeat 2 dig = buf shr ((2-%)*4) and 0Fh+'0' if dig > '9' dig = dig-'0'+('A'-10) end if display dig end repeat display ' ' end if end repeat display ' |' ;display ascii data repeat $10 if curptr+(%-1) < b | e <= curptr+(%-1) display ' ' else load buf byte from spaceptr+(%-1)+(curptr-b) if buf < $20 match np,np_s \\{ display \\`np \\} else display buf end if end if end repeat display '|',13,10 curptr = curptr + $10 end repeat end if spaceptr = spaceptr+(e-b) end while end virtual purge org,virtual restruc virtual restore begin_s, base_ns, mload_s, base_s, awidth_ns, np_s purge dispmem_ \} } This one may seem to be a bit complicated, but I actually consider it to be of high quality, cause it's from time when I finally came up with a universal architecture for paired macros, which does not introduce any side effects. What I wanna say is that it's not just for a subset of users, and everyone may find it useful and very easy to use: Code: dispmem_ ;any code, any data, any other macros here (limited by common sense, you know) _dispmem It can be used with any code and won't interfere with other macro architectures. And no one is gonna think like "may be it produces suboptimal code and I could invent the wheel again and do better", because it does not produce any code. Similarly code encryption, formatting, structure definition, data allocation and reallocation etc. macros can have same constraints. However macros that produce some necessary amount of code can also be very handy (like the proc or invoke macro) and I by no means say, that macros must not produce code, because that is actually their initial and primary purpose. P.S. The above macro is better to use with the console version of fasm with a third party GUI (like notepad++) configured to display monospaced font, cause fasmw has a very small and not sizable display window. _________________ Faith is a superposition of knowledge and fallacy |
|||
![]() |
|
fasmnewbie 26 Feb 2014, 15:42
l_inc wrote: fasmnewbie Oh man, you are speaking biohazard again. This is baldr, revolution and privalov's language when they sleepwalking. Not mine ![]() But that conversion ifs looks nice to fit into my macro set ![]() |
|||
![]() |
|
l_inc 26 Feb 2014, 18:18
fasmnewbie
Quote: you are speaking biohazard OK. Let me try again. The whole biohazard from the first block of code should be completely saved in a separate file, say "general.inc", together with whatever other biohazards you wanna be there. And here's the nature friendly and as clear as bidistillate usage: Code: include 'general.inc' use32 dispmem_ org 0 db 'Hello, I''m not a biohazard. I''m simple and clear' org 0 db 'Hmm... Is it possible to multiply by 4 in printable ASCII x86?' org $100 imul eax,4 org 0 db 'No. No luck here' db 'Maybe like this?' org $100 add eax,eax add eax,eax org 0 db 'Nope. This does not fit again' org $100 shl eax,2 org 0 db 'Wtf?! All the time these characters below 0x20!' org $100 shl eax,1 shl eax,1 org 0 db 'Yuhuu! Now I can start writing my own exploit!' _dispmem Compile with fasm.exe (not fasmw.exe!) and look at the compilation output. _________________ Faith is a superposition of knowledge and fallacy |
|||
![]() |
|
l_inc 26 Feb 2014, 20:03
fasmnewbie
Quote: I am still missing random number utility. Math is really not my thing I missed this one. One doesn't really need much math in order to implement existing algorithms. There are plenty of well-studied comprehensively described PRNGs with very good characteristics. However depending on whether you need a runtime or compile time PRNG, you might confront a seeding problem. E.g., %t is not a dreamlike source of entropy. _________________ Faith is a superposition of knowledge and fallacy |
|||
![]() |
|
revolution 26 Feb 2014, 20:08
Code: include 'http://random.org' |
|||
![]() |
|
m3ntal 27 Feb 2014, 03:51
fasmnewbie: Standard functions/proc/edures (push-call) are recommended for library routines (example: memcpy, printf). Macros are perfectly suitable for language features/constructs that only produce a few instructions - variables, arithmetic, comparisons, loops, etc - but they can be dangerous, wasteful and not good for library routines that are reusable 2+ times... except when testing.
Example: In this macro, eax/ecx cannot be sent as parameters. If it were a function, it would not matter since it would access a copy of the register values on the stack (not that it should be a function, only an example): Code: macro zero.bit v, i { ; v&=~(1<<i) mov eax, 1 mov ecx, i shl eax, cl not eax and v, eax } |
|||
![]() |
|
fasmnewbie 27 Feb 2014, 16:25
m3ntal wrote: fasmnewbie: Standard functions/proc/edures (push-call) are recommended for library routines (example: memcpy, printf). Macros are perfectly suitable for language features/constructs that only produce a few instructions - variables, arithmetic, comparisons, loops, etc - but they can be dangerous, wasteful and not good for library routines that are reusable 2+ times... except when testing. I offer no counter arguments of what you are saying m3ntal. But I have different variables to satisfy. While people here are talking about how many bytes and clock cycles they can save using a proc against a macro, my variables are how many teaching hours/cost one can save by finding a teaching methods that are; 1. clean, with minimal distractions 2. fast and easy 3. effective Thats my job and my objective. And one method I can find is through the use of macros, particularly when dealing with 16-bit programs and the target clients are students with HLL background. I hold no teaching position by the way. My job is to oversee pedagogical matters. I see your macro has one AND instruction. One student could ask "what's the effect of ANDing v against (E)ax. Can you prove it to me?" This is how a macro call can do the job best. A simple clean 5-line code; Code: org 100h include 'lib16.inc' mov ax,1001110110110110b prtreg ax,-b and ax,0FF00h prtstr "==> and ax,0FF00H\n" prtreg ax,-b exitp The output. Descriptive and visual: Code: ax:9DB6 [10011101 10110110] ==> and ax,0FF00H ax:9D00 [10011101 00000000] This is how fast, clean and effective it can be by using macros. Short, simple, descriptive and with minimum unnecessary distractions (push, pop, calls, and the procs and the likes just to learn AND by visual approach). Imagine the time that can be saved this way. And for the whole semester (some even offer only half-semester assembly course!), a teacher/instructor and the students can use only 2 files to learn almost all about popular x86 instructions using this way. You have no idea how this can greatly save teaching preparations costs, instructors burden and time. Now that's my variables. My main business is pedagogy. ANd from pedagogy point of view, this "proc" approach is messy and distracting until its time to really learn about 16-bit stack and procedures. Code: org 100h mov ax,1001110110110110b push ax ;<--- unnecessary distactions call prtreg pop ax ;<--- unnecessary distractions and ax,0ff00h ;<--- just to learn this! push boring call prtstr pop whatever ;needs to be popped push ax ;<--- unnecessary distractions call prtreg push 0 ;<--- more distractions call [exit] boring db "==> and ax,0FF00H\n" ;<---- not cool prtreg: looong dirty distraction... ret prtstr: another miles long distractions ... ret exit Remember, we are talking about 16-bit FASM assembly here without using and extended header (not to mention a linker ![]() |
|||
![]() |
|
l_inc 28 Feb 2014, 11:19
fasmnewbie
Overquoting is a bad habit. _________________ Faith is a superposition of knowledge and fallacy |
|||
![]() |
|
fasmnewbie 28 Feb 2014, 13:35
l_inc wrote: fasmnewbie Like dispmem [here] ... [there] dispmem. Is it even possble because I see itresembles a debugger output. |
|||
![]() |
|
fasmnewbie 28 Feb 2014, 13:57
oh man, Your biohazard is nasty!!!
|
|||
![]() |
|
l_inc 28 Feb 2014, 14:02
fasmnewbie
I don't really understand the problem. You can enclose almost any part of your listing into the dispmem_ ... _dispmem block. And you can do it multiple times for different pieces of your listing. Do you mean, you'd like to specify an address value and size without enclosing the specified block into the "dispmem parentheses"? _________________ Faith is a superposition of knowledge and fallacy |
|||
![]() |
|
fasmnewbie 28 Feb 2014, 15:23
l_inc
u dont have to. I just figured out how it works. Thanks for your biohazard. Thats a mastrpiece man! Supercool ![]() |
|||
![]() |
|
sid123 27 Mar 2014, 10:45
The topic that I am riding on has derailed.
|
|||
![]() |
|
fasmnewbie 31 Mar 2014, 02:57
sid123 wrote: The topic that I am riding on has derailed. sid123 Sorry. Kind of busy lately. Do you need the complete source? Don't have time to clean up my macro set. But it goes something like this. Code: ;------------------------ macro fprint fmt*,[args*] ;------------------------ { common pusha if fmt eqtype '' fprts fmt,args else fprtv fmt,args end if popa } ;FPRINT sub macro ;do not use directly macro fprts fmt*,[args*] { reverse push args common local .loop,.cha,.oct,.intg,.b local .bin,.hex,.ok,.line,.done,.sig xor si,si mov ah,0eh .loop: cmp byte[cs:.b+si],0 je .done cmp word[cs:.b+si],'\n' ;new line je .line cmp word[cs:.b+si],'%d' ;for signed je .sig cmp word[cs:.b+si],'%u' ;for unsigned je .intg cmp word[cs:.b+si],'%b' ;for binary je .bin cmp word[cs:.b+si],'%h' ;for hex je .hex cmp word[cs:.b+si],'%o' ;for octal je .oct cmp word[cs:.b+si],'%c' ;for char (dw) je .cha mov al,byte[cs:.b+si] int 10h inc si jmp .loop .cha: pop bx prtchar bl jmp .ok .oct: pop bx prtoct bx,-line jmp .ok .hex: pop bx prthex bx,-line jmp .ok .bin: pop bx prtbin bx,-t,-line jmp .ok .intg: pop bx prtint bx,,-line jmp .ok .sig: pop bx prtint bx,-s,-line jmp .ok .line: line .ok: add si,2 jmp .loop .b db fmt,0 ;local string .done: } ;FPRINT sub-macro ;do not use directly macro fprtv fmt,[args] { reverse push args common local loo1,cha1,oct1,intg1,sig1 local bin1,hex1,ok1,lin1,done1 mov si,fmt mov ah,0eh loo1: cmp byte[si],0 je done1 cmp word[si],'\n' je lin1 cmp word[si],'%d' je sig1 cmp word[si],'%u' je intg1 cmp word[si],'%b' je bin1 cmp word[si],'%h' je hex1 cmp word[si],'%o' je oct1 cmp word[si],'%c' je cha1 mov al,byte[si] int 10h inc si jmp loo1 cha1: pop bx prtchar bl jmp ok1 oct1: pop bx prtoct bx,-line jmp ok1 hex1: pop bx prthex bx,-line jmp ok1 bin1: pop bx prtbin bx,-t,-line jmp ok1 intg1: pop bx prtint bx,,-line jmp ok1 sig1: pop bx prtint bx,-s,-line jmp ok1 lin1: line ok1: add si,2 jmp loo1 done1: } Thanks to baldr and l_inc Usage: Code: fprint "Your numbers are %u,%o and %d\n",[x],43h,-55 or Code: fprint mystr,1011b,[y],177777o mystr db "Hello\n%u\n%d\n%h\n",0 y dw -34h Edit: not %d but %u for unsigned. |
|||
![]() |
|
sid123 21 Apr 2014, 12:08
well i highly suggest you to put all of your system dependent (putchar, readchar, open, close, write, read etc.) functions inside another file called "syscalls.inc" or whatever you prefer, this way it'll be easier for others to understand and use.
![]() |
|||
![]() |
|
fasmnewbie 03 May 2014, 16:44
sid123 wrote: well i highly suggest you to put all of your system dependent (putchar, readchar, open, close, write, read etc.) functions inside another file called "syscalls.inc" or whatever you prefer, this way it'll be easier for others to understand and use. I'll see what I can do. It may take me some time to read my own code though. Left it for quite some time. Now I am having trouble understanding what I wrote. LOL. |
|||
![]() |
|
Goto page Previous 1, 2 < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.