JavascriptProva

sabato 14 settembre 2013

Ripassare l'Assembly!

Ed eccoci al ripasso dell'Assembly... mi sembra di non ricordare più quasi niente...

Il tutorial è questo.

Il codice è questo, ricopiato pedissequamente dal tutorial:
.386
.MODEL Flat,STDCALL
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib 
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
.DATA
MsgBoxCaption  db "Iczelion Tutorial No.2",0
MsgBoxText       db "Win32 Assembly is Great!",0
.DATA?
.CONST
.CODE
start:

invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK 
    invoke ExitProcess,0
end start
che viene assemblato e linkato così, seguendo pedissequamente le istruzioni del tutorial:
C:\masm32>ml /c /coff /Cp prova.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: prova.asm

C:\masm32>ml /c /coff /Cp prova.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: prova.asm

***********
ASCII build
***********


C:\masm32>link /SUBSYSTEM:WINDOWS /LIBPATH:c:\masm32\lib prova
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


C:\masm32>
senza errori, dunque...

Lo avvio:
C:\masm32>prova

C:\masm32>
...e mi dà la messagebox... che carina!


Adesso mi faccio un viaggio all'interno di windows.inc e vediamo cosa c'è... Incontro la definizone delle costanti NULL e MB_OK... Sta' a vedere che questo file viene incluso soltanto per avere le definizioni delle costanti? Ho vaghe reminiscenze che...

Bene. "Commentiamo" la riga del codice che include windows.inc e vediamo che succede.

.386
.MODEL Flat,STDCALL
option casemap:none
;include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib 
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
.DATA
MsgBoxCaption  db "Iczelion Tutorial No.2",0
MsgBoxText       db "Win32 Assembly is Great!",0
.DATA?
.CONST
.CODE
start:

invoke MessageBox, NULL, addr MsgBoxText, addr MsgBoxCaption, MB_OK 
    invoke ExitProcess,0
end start
...assembliamo...
C:\masm32>ml /c /coff /Cp prova.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: prova.asm
prova.asm(17) : error A2006: undefined symbol : MB_OK
prova.asm(17) : error A2114: INVOKE argument type mismatch : argument : 4
prova.asm(17) : error A2006: undefined symbol : NULL
prova.asm(17) : error A2114: INVOKE argument type mismatch : argument : 1

C:\masm32>
Bene: riga 17 ottengo "simbolo indefinito: MB_OK" e un errore relativo agli argomenti di INVOKE, precisamente all'argomento 4. Stessa cosa per il simbolo NULL, argomento 1.
Questo conferma la mia reminiscenza circa il fatto che nel windows.inc siano contenute le costanti.
Bene, andiamo a vedere il valore delle costanti sul file windows.inc...
Copio uno stralcio del file nel quale è presente NULL:
TRUE                                 equ 1
FALSE                                equ 0
NULL                                 equ 0
Normal                               equ 000000h
ReadOnly                             equ 000001h
Bene, NULL è pari a zero.

Copiamo un altro stralcio dove è presente la costante MB_OK:
SB_DISABLE_DOWN                     equ 2h
ESB_DISABLE_LTUP                     equ ESB_DISABLE_LEFT
ESB_DISABLE_RTDN                     equ ESB_DISABLE_RIGHT
MB_OK                                equ 0h
MB_OKCANCEL                          equ 1h
MB_ABORTRETRYIGNORE                  equ 2h
MB_YESNOCANCEL                       equ 3h
MB_YESNO                             equ 4h
MB_RETRYCANCEL                       equ 5h
MB_ICONHAND                          equ 10h
MB_ICONQUESTION                      equ 20h
Bene, MB_OK è pari a zero.

Allora, se la sua funzione in questo caso è solo quella di "tradurre" le costanti in valori numerici, dovrei rendere inutile l'inclusione di windows.inc se sostituisco nel codice sorgente questi simboli con i loro valori numerici.
.386
.MODEL Flat,STDCALL
option casemap:none
;include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib 
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
.DATA
MsgBoxCaption  db "Iczelion Tutorial No.2",0
MsgBoxText       db "Win32 Assembly is Great!",0
.DATA?
.CONST
.CODE
start:

invoke MessageBox, 0, addr MsgBoxText, addr MsgBoxCaption, 0 
    invoke ExitProcess,0
end start
Salvo e provo a riassemblare:
C:\masm32>ml /c /coff /Cp prova.asm
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: prova.asm

C:\masm32>
Perfetto! Allora linkiamo pure e vediamo se funziona.
C:\masm32>link /SUBSYSTEM:WINDOWS /LIBPATH:c:\masm32\lib prova.obj
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

LINK : fatal error LNK1104: cannot open file "prova.exe"

C:\masm32>
AYAYAYAYAY!!!!! E che è successo???

Un momento! Il programma è in esecuzione! E certo! E' una causa frequente di errore nel linkaggio!
Chiudo la MessageBox che sta ancora lì...

C:\masm32>link /SUBSYSTEM:WINDOWS /LIBPATH:c:\masm32\lib prova.obj
Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


C:\masm32>
Benissimo. Il problema era proprio quello.

Faccio partire il programma.
C:\masm32>prova

C:\masm32>
Ed ecco che funziona egregiamente!

Nessun commento:

Posta un commento