News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

jump help...

Started by j00n, July 20, 2010, 04:50:57 AM

Previous topic - Next topic

j00n

I'm messing around with the included listbox example... examples/exampl01/listbox

i've added a new jmp labelled @@@2... but without return 0 it continues to execute @@@1...

is this the right way of doing things?



jmp @@@1

           lItem1 db "Roses are red,",0
           lItem2 db "Violets are blue.",0
           lItem3 db "If sugar is sweet,",0
           lItem4 db "What happened to you ?",0



@@@2:



    mov ecx, 0
   
    .REPEAT
    inc ecx
    push ecx
   

          invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem1
       
       ;  invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem2
       ;  invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem3
       ;  invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem4
             
    pop ecx
   
.UNTIL ecx==500

return 0 ;if this isn't here... it will continue

@@@1:
     

       invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem1
       invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem2
       invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem3
       invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem4



basically what i'm trying to ask... (this stuff is hard to explain haha)

are labels alone enough to stop execution of a jmp?


jmp @@@1

;some stuff here...

@@@1:
mov eax, 123

@@@2:
mov eax, 321


will the jump continue to @@@2 ?

KeepingRealBusy

No, labels are the target of the jump not the end of it. Yes, it will continue.

j00n


thanks for the fast reply... I got the outcome I wanted by making a proc...

I made the "about > help..." menu items "invoke doit"

doit proc

    mov ecx, 0
   
    .REPEAT
    inc ecx
    push ecx
   

          invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem1
       
       ;  invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem2
       ;  invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem3
       ;  invoke SendMessage,hList2,LB_ADDSTRING,0,ADDR lItem4
             
    pop ecx
   
.UNTIL ecx==500
return 0

doit endp




my last question would be is there a trap for the right or wrong place for a proc...?

does it have to be in any specific order in the program or is that what the PROTO is for?



KeepingRealBusy

There are no proc order requirements.

Prototypes are for defining function call parameters for functions in libraries or DLLs. You do not need them for procs you define in your assembly.

You need one more thing in the file to satisfy the linker so that you can make it an executable, the last line should be an "END doit" to tell the loader where the start of the program is. This can be a proc name (doit in you case), or just a label.

Dave.

hutch--

j00n,

Every PROC must have a RET to ensure that the program execution is returned back to the last CALL. In the pseudo high level notation in MASM and INVOKE contains a CALL mnemonic and with the MACRO "return" with a value of zero you have a RET mnemonic (properly RETN).


ItemName PROC args etc ....

    ; your code

    ret

ItemName ENDP
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

j00n

gotcha...  thanks for the help, i've got another one for you

in the loop... how would I get the numeric value of ecx and add those numbers to the listbox?

all I got in my attempts were garbage data...

Ghandi

ECX is tagged as a *scratch* register and as such when returning from a calli to any win32 api the state of the register is undefined at best (might luck through without it being trashed) or guaranteed to be trashed. Iif you want to preserve or resuse the value you need to save it somewhere, such as the stack (local variable or pushed to the stack) or a global variable.

HR,
Ghandi

hutch--

j00n,

If you have the value in ECX, you need to convert it to a string THEN copy it to the list box. Now just be careful as ECX can be modified by Windows API calls so make sure that if you are transferring the value from ECX through a conversion function that you don't mess up the value in ECX before its converted. Just as a safety issue its better to use EBX ESI or EDI as they are not modified by a Windows API call.

You can use the ECX register directly as long as you don't run any code that changes it before it is converted.

This is the logic here.


mov ecx, value

invoke API Function

; result ECX may be modified by the API call.
; -------
mov esi, value

invoke API Function

; result = ESI remains unchanged


You can also put the value in ECX into a normal stack LOCAL variable and it is safe from being modified.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

j00n

I'm using ebx now... I don't quite understand how to convert the number to a string... do I need to use num2str ?

I'm a little lost... a little might be an understatement... somebody point me in the right direction please!

I really appreciate the help

hutch--

OK, If you are going to use EBX in your procedure, make sure you PUSH it near the start of the procedure and POP it at the end before the RET.


YourFunc proc args etc ....

    push ebx

  ; your code using EBX

    pop ebx

    ret

YourFunc endp


Now for number to string conversions you can try either the macros str$(), ustr$() or go to the MASM32 Library help file and check out the conversion procedures that convert a DWORD (EBX is a DWORD in size) to a string. Note that to do this you need to create a local buffer for BYTE data.

At the top of the proc BEFORE any code try something like this.


LOCAL buffer[64]:BYTE    ; 64 byte buffer for string data.

To use its ADDRESS use this type of code.

lea eax, buffer

The ADDRESS of the local buffer is now in EAX
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

j00n

I still didn't get it at first but read a few more examples and I think I got it... still tinkering... thanks for everything so far!

hutch--

j00n,

The first bit (register preservation) is an important part of assembler programming for a very sensible reason. With the 8 general purpose registers they are a limited resource but still do the lion share of processing on an x86 processor. To ensure an effieient usage of these registers they were split into 2 classes, 3 expendable registers, 3 that need to be preserved and two that are usually but not always used to construct a procedure, ESP and EBP.

The rule is called the Intel ABI (Application Binary Interface) and it sets out a consistent way of sharing registers between the operating system and the running application.

The expendable registers are EAX ECX EDX, the ones that require preservation between function calls are EBX ESI EDI, ESP and EBP are normally used to create a stack frame. There are variations that use procedures that have no stack frame, mainly to get another register EBP but that is a bit more advanced.

This is an important thing to understand as if you get it wrong you have one procedure overwriting a register that holds an important value and your code crashes. Many have been foolish enough to try shortcuts and often they work but the next version of the OS may do it differently and the app crashes. Write reliable code that conforms to the Intel ABI and your code runs properly on all OS versions as they are written to conform to the Intel ABI in the first place.

Have a good read of the help file in MASM32 ASMINTRO Help as it contains a good description of how this works. It sounds complicated but its in fact no big deal if you start off the right way in the first place.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

j00n

hey I finally got it... I used dwtoa and a buffer and it displays in a messagebox for testing... I'm gonna see if i can get it into the list... :P

I got slowed down by a noob error I guess instead of "addr buffer" I used just buffer... gotta keep reading those help files they are really great....

Edit: this is kinda a multi-topic thread so I meant I got the number in the loop to be displayed in a messagebox... (ebx) what i'm using now...