Whats wrong with general purpose register

Started by Khurram, October 08, 2006, 06:35:48 PM

Previous topic - Next topic

eek

If you are a total newbie then I recommend this integrated 16 bit interpreter/debugger/compiler

http://www.btinternet.com/~btketman/tutpage.html

Its an interactive asm tutorial that does most 16bit stuff and will at least let you see what is going on as you write, you can run and change stuff without compiling the code because it's an interpreter.

Synfire

a. @data is a directive which is used by MASM to represent the .data segment.

b. Because that is how the intel opcodes are designed. Try using this, from Biterider's OA32 package:
m2m macro DstMem:req, SrcMem:req, Reg
    ifb <Reg>
      push SrcMem
      pop DstMem
    else
      mov Reg, SrcMem
      mov DstMem, Reg
    endif
endm

   that way, you can use 'm2m ds, @data' to preform the push/pop version, or use 'm2m ds, @data, ax'
   to use the AX register to move them.

c. Like I said above, there is no opcode for it, so you have to either use the push/pop method or move
   through AX method.

d. AX, like you mentioned before, is the prefered opcode. But you can use any of the general purpose
   registers such as BX, CX, or DX if you want. It's just customary to use AX.

Regards,
Bryant Keller

MichaelW

Khurram,

If you were to try and move a constant into a segment register, for example with either of these instructions:

mov ds, @data
mov ds, 123

MASM would return "error A2070: invalid instruction operands" because the processor does not have an instruction that can do this. If you were to look in the opcodes.hlp file that is included in the MASM32 package, at the MOV instruction, you would see only two forms of the instruction where a segment register is the destination operand:

mov segreg, reg16
mov segreg, mem16

But when the destination operand is a general-purpose register you would see three forms:

mov reg, reg
mov reg, mem
mov reg, immed

A mov segreg, immed form would be needed to move a constant into a segment register, but the processor does not support this form.


@data is one of the predefined symbols that MASM automatically defines and assigns a value to. When your source code is assembled, at every place in the code that one of these symbols appears, MASM replaces the symbol with its assigned value. The value that MASM would assign to @data would depend on the segment structure of the program, which in turn would depend on the memory model, program layout, version of MASM, etc, but a typical value for a small model might be 05E1h.

For more information see Predefined Symbols here.

To see what the .STARTUP and .EXIT directives do, see Staring and Ending Code with .STARTUP and .EXIT here

eschew obfuscation

Khurram


Thanx a lot Bryant Keller and michael i got yours point i have one more problem
with ah,9 service i know ah,9 service requires that we have the segment of
the text/string in DS and the offset in DX.But when i put remarks for getting
segment of string in DS from one of my code it doesnt works it gives me
garbage as an output,while in other code if i do the same it doesnt give me
any garbage instead it works nicely why?


Code which doesnt work upon puttintg remarks to get segment of string into DS.

.model small
.stack  100h
.data
str1  db 'Khurram$'
str2  db 'Siddiqui$'
.code
  main proc
   mov ax,@data
;  mov ds,ax
   mov dx,offset str1
   mov ah,9 ;Int 21h, subfunction 09h can be used to print a string on the
            ;screen. Here,you'll need to put the number 09h in the AH
            ;register, the segment address of the string in DS and the
            ;offset address of the string in DX. The string must be
            ;terminated with a $ sign, so if you want to print you name
            ;"Khurram!" on the screen, the string should be: Khurram!$.
            ;This example routine could print such a string on the screen
            ;for you.
            ;--
            ;OR
            ;For this interrupt it requires that we have the segment of
            ;the text/string in DS and the offset in DX
   int 21h
   mov ah,4ch
   int 21h
  main endp
end main



Code which work upon puttintg remarks to get segment of string into DS.

.model small
.stack  100h
.data
var db 7 dup(0)
.code
main proc
        mov ax,@data
;       mov ds,ax

        mov cx,7
        mov si,offset var

        lbl:
        mov ah,1
        int 21h
        mov [si],al
        inc si
        loop lbl

            mov byte ptr [si],'$'
            mov ah,9
            mov dx,offset var
            int 21h
            mov ah,4ch
            int 21h
main endp
end main


Thanx again

Khurram

MichaelW

The second code does not display garbage because it is loading the characters you input into the memory that is being displayed. If you remove the code that is loading the characters into the memory, then the remaining code will display garbage.

When the loader loads your program it sets DS to the segment address of the Program Segment Prefix (PSP). The PSP is a 256-byte block of memory that precedes the program in memory. The OS uses the lower 128 bytes of the PSP to store various values that it uses to manage the program, and the upper 128 bytes to store the program's command line. OFFSET var is returning 0Eh, so if DS is set to the segment address of the PSP then the memory that the input characters are being stored in, and the memory that is being displayed, is at offset 0Eh in the PSP. If you load a limited number of characters into this memory and terminate the characters with a "$", then Interrupt 21h Function 9 can correctly display those characters. If you do not load characters into this memory, and you load DX with 0Eh and call Interrupt 21h Function 9, it displays whatever is in the memory, continuing until it encounters a "$", or under Windows until Windows kills the program, or under real DOS until the system crashes. The first 100 or so bytes will appear as garbage because they are actually binary data that the OS stored in the PSP.

Starting at offset 0Eh in the PSP, the OS stores three DWORD values that it uses when the program exits. When your code uses this area of memory to store characters, it will not cause an immediate problem, but under real DOS it likely would cause a problem when the program exits. As a general rule, your program should avoid writing to memory that it does not own.
eschew obfuscation

Khurram

Thanx Micahel i got yours point a little bit but will ask you laterz about it more.

I have request to Admin of this forum please dont DELETE this post if its inactive for some days
cause i came around with some other urgent work for that i will be away from this forum
for somedays ,after resume i will use this same thread to ask questions,all guys helped me a lot i
was not able how to proceed but Thanx to all they pushed me in good direction now i am
enjoying assembly :)


will back very soon with some more stupid problems


Thanx all you guys

Khurram

Khurram

hi allz guys,

I have problems regarding understanding about branch prediction?

I surfed the google and i read about it but still not getting good understanding ,Please share some
thought about it?

Khurram

Synfire

Khurram,

When a program is being executed by the processor, the processor assumes that certain jump will be taken and others won't. This lets it preload the next instruction after the branch to allow for speedy execution. When the prediction is wrong, then there is a slight stall in execution. The rule here is, conditional jumps which jump backwards are assumed to be taken. Conditional jumps which jump forward are assumed not to be taken. Instead of trying to organize my code to fit branch predictions, I tend to set the predictions myself using the following macros:


; Branch Hint: Taken
BHT MACRO
    DB 2Eh
ENDM

; Branch Hint: Not Taken
BHNT MACRO
    DB 3Eh
ENDM


This way, if I have a conditional statement which usually jumps forward, I can use 'BHT' as though it's a mneumonic just before the conditional jump to fix prediction therefore removing any stall. I use these macros a lot when doing my WndProc because certain conditional jumps (like the one going to WM_COMMAND) will be executed a lot. Since I like to keep the Messages below my comparsons, I mearly change the line from:

cmp EAX, WM_COMMAND
jz @@wm_command


to:

cmp EAX, WM_COMMAND
BHT
jz @@wm_command


It does add one byte to the code, but it removes the stall so execution is faster. On messages like WM_CREATE, I leave those as normal to avoid using an extra byte, since that jump will only be taken once.

The reason branch hints work the way they do is to optimize for looping. since during loops you generally jump backwards. Another common use for BHT would be done during error checking, believe it or not, most applications I see have a stall when they check for an error because they normally jump forward but don't put any override to inform the processor of this, so the processor assumes the jump will fail and stalls when it doesn't fail.

A good source of information on this is either Anger Fog's site or (the source I use) the "AI-32 Intel Architecture Optimization Reference Manual". The Intel Manuals are available in PDF format from various points on the internet, searching google for the name of the book should return results.

I hope this has helped you.

Regards,
Bryant Keller

Mark Jones

Quote from: eek on October 18, 2006, 10:29:41 AM
If you are a total newbie then I recommend this integrated 16 bit interpreter/debugger/compiler

http://www.btinternet.com/~btketman/tutpage.html

Its an interactive asm tutorial that does most 16bit stuff and will at least let you see what is going on as you write, you can run and change stuff without compiling the code because it's an interpreter.

Wow Eek, that's a cool little program! :bg
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

ramguru

Quote from: Mark Jones on August 25, 2007, 10:00:33 PM
Quote from: eek on October 18, 2006, 10:29:41 AM
If you are a total newbie then I recommend this integrated 16 bit interpreter/debugger/compiler

http://www.btinternet.com/~btketman/tutpage.html

Its an interactive asm tutorial that does most 16bit stuff and will at least let you see what is going on as you write, you can run and change stuff without compiling the code because it's an interpreter.

Wow Eek, that's a cool little program! :bg

I remember this app  :lol
It's reaaaaaaaally old! I even don't remember if it helped me to understand asm better, but it was first I was learning the machine language from...

eek

Its runs on an old HP200LX as well, so you can play with it anywhere.

I'm a bit obsessed with not being trapped in front of a desk, but I want to be able to do widgets on the beach without needing a Pentium3000 triple core laptop.

I've discovered another one recently.

If you get one of these
http://www.pk3.org/Astro/index.htm?pv_software.htm

And download the0402 version
http://pandora.inf.uni-jena.de/p/e/noo/owbasic/basic.html

pop the OWBasic.bin file into the PVs460 unit and you can do Basic programming on an ultra light palmtop with 100+ hours of battery life.