View Full Version : Assembly language question
MouseTrap
26-09-2009, 08:15
:roll:
This is pretty far off topic for Thinbasic, But I thought i would give it a shot as there's lots of smart people here.
I'm trying to get some info about *how and *why the flags register gets updated for certain commands.
Ive searched over the net pretty thoroughly, but All ive been able to come up with is This http://faydoc.tripod.com/cpu/cmp.htm page that shows *what flags get altered from certain commands. But i still find anything about the rules of how/why.
For example, The JL command will jump if (sf <> of), but why would those flags get set in the first place, and why those flags?
Can anyone help point me in the right direction?
sorry for being off topic.
Charles Pegge
26-09-2009, 09:15
Hi Mousetrap,
Good question. It's quite easy really. Generally speaking all arithmetic and logical operations set flags. MOV PUSH POP CALL JMP and most other instructions do not. The most important flags are ZF zero flag, SF sign flag and CF carry flag. Conditional jumps and moves use these flags to determine whether they execute or not.
For floating point: FCOMI and FCOMIP, FUCOM and FUCOMIP are the only instructions affecting the ZF and CF CPU flags directly.
Charles
The ultimate reference manuals: (Volumes 2A and 2B for the instruction set)
http://www.intel.com/products/processor/manuals/
MouseTrap
26-09-2009, 09:32
Thanks Charles!
I'll definitely check those out, The reason I ask is that I've written an x86 ASM interpreter and VM. I want to keep everything faithful to the way things work internally and flags the only thing holding me up. Most info on the net skips over the details of *why they change.
Charles Pegge
26-09-2009, 10:00
I hope your project goes well, and will help to make x86 Assembler less intimidating. - Actually, Asm lost most of its bite when the GPF was invented. Crashing the PC is quite rare these days - but a virtual CPU gives you an easier way to step through your code and see what is happening.
Charles
MouseTrap
26-09-2009, 10:19
Thanks!
the project is my backwards attempt to learn assembly, By writing all of the error handling and conditions for each instruction i'm learning a lot of valuable info. When the instruction set gets more filled out, i'll be releasing it. maybe as a dll so others can put there own front end on it. (Thinbasic?)
It sounds like you are way beyond this MouseTrap, but you might still enjoy this:
http://video.google.com/videoplay?docid=7654043762021156507#
MouseTrap
27-09-2009, 08:33
Thats a great lecture, I would definitely take that course!
Thanks
Charles Pegge
27-09-2009, 10:07
This is a simple idea for stepping through Asm for real. Oxygen allows Basic and Asm to be freely mixed so you can embed messages and other diagnostics with appropriate register protection.
Charles
'
'----------------------------------
'TRACING EXECUTION OF ASSEMBLY CODE
'==================================
Uses "oxygen"
Dim src As String
src="
#basic
'----------------
'DIAGNOSTIC MACROS
'================
def SHOW pushad : mov a,%1 : print `%1: ` hex a : popad
def MSG pushad : print %1 : popad
def FLAGS pushad : pushf : pushf : pop eax : mov a,eax : print `Flag Register: ` hex a : popf : popad
dim a
'--------
'ASM TEST
'========
mov ecx,0
mov eax,1
cmp eax,0
FLAGS
jz nif
MSG `condition Not met`
mov ecx,1
nif:
SHOW ecx
"
'MsgBox 0,O2_PREP src
O2_BASIC src
If Len(O2_ERROR) Then MsgBox 0,O2_ERROR : Stop
O2_EXEC
MouseTrap
27-09-2009, 10:35
Thanks Charles.
Thats pretty sharp.
I'm assuming there is nothing like a callback function that could be set for oxygen that could run with each instruction to inspect the regs at runtime?
Charles Pegge
27-09-2009, 12:04
Well not quite, but this might be more useful - you can mark each instruction with an X wherever you want to see the register contents:
'
'-----------------------------------
'TRACING EXECUTION OF ASSEMBLY CODE
'INSPECT REGISTERS AFTER INSTRUCTION
'===================================
Uses "oxygen"
Dim src As String
src="
#basic
'----------------
'DIAGNOSTIC MACROS
'================
def SHOW pushad : mov a,%1 : print `%1: ` hex a : popad
def MSG pushad : print %1 : popad
def FLAGS pushad : pushf : pushf : pop eax : mov a,eax : print `Flag Register: ` hex a : popf : popad
def x : pushad : pushf : call showregs : popf : popad :
dim a
sub showregs()
dim as long v(9) at [ebp+8]
dim as string tab=chr 9
print `
Registers:
EAX: ` tab hex (v(9)) `
ECX: ` tab hex (v(8)) `
EDX: ` tab hex (v(7)) `
EBX: ` tab hex (v(6)) `
ESP: ` tab hex (v(5)) `
EBP: ` tab hex (v(4)) `
ESI: ` tab hex (v(3)) `
EDI: ` tab hex (v(2)) `
EFLAGS: ` tab hex (v(1)) `
`
end sub
'--------
'ASM TEST
'========
'PLACE `x` AT ANY INSTRUCTION TO SEE THE REGISTER CONTENTS
mov ecx,0
x mov eax,1 X
cmp eax,0 X
'FLAGS
jz nif
'MSG `condition Not met`
mov ecx,1
nif:
'SHOW ecx
"
'MsgBox 0,O2_PREP src
O2_BASIC src
If Len(O2_ERROR) Then MsgBox 0,O2_ERROR : Stop
O2_EXEC
MouseTrap
27-09-2009, 21:15
This is great. Thanks