- UID
- 32468
注册时间2007-6-1
阅读权限10
最后登录1970-1-1
周游历练
该用户从未签到
|
以下源码主要引自第八阁论坛:http://bbs.chinadbg.cn/forum-7-1.html
也有部分代码是飞翔技术论的朋友给改过的。
我只是来了一个开始,每个人都有权,发出他的标题,但要负责他的标题。
从基础开始学习高级语言+汇编+反汇编
一个简单的小学算术从1加到10[设计思路,用循环做1到10的循环,然后种用循环变量垒加和]从1加到10,请高手们写出高级语言+汇编+反汇编,代码
还有要注意,我写程序的错误,因为这才是真正要让大家学习的地方。
非常高兴看到大家的支持,你们发的程序,请在第二天加上注释。(主要是有错的程序)
以下是C的代码
- #include "stdio.h"
- main()
- {
- int i,j;
- for(i=1;i>10;i++) /*以下为循环*/
- {
- j=j+i
- }
- printf("%d\n"j);
- getch();/*可以让程序暂停*/
- }
复制代码- 以下是修改后的代码
- #include "stdio.h"
- main()
- {
- int i,j;/*这里要给变量初始化:int i=j=0*/
- for(i=1;i>10;i++) /*这个循环中,i>10应该写成i<=10或i<11*/
- {
- j=j+i/*这里要加个“;”:j+=i;*/
- }
- printf("%d\n"j);/*这里要在"%d\n"和j中间加上“,”*/
- getch();/*可以让程序暂停*/
- }
复制代码 C++代码,注意C++可以随用变量,随定义、初始化- #include "stdio.h"
- void main()
- {
- int icount = 0;
- for (int i = 1; i <= 10; i++)
- {
- icount += i;
- printf("%d\n", icount);/*如果你是想打印每次结果可以这样写,如果想只打印最后的结果,这句应该移到“}”外面*/
- }
- }
复制代码 java代码- public static void main(String[] args){
- int count = 0;
- for(int i=1;i<=10;i++){
- count = count+i;
- }
- System.out.println("The sum of 1 to 10 is "+count);
- }
复制代码 Delphi代码- Delphi
- Var
- i,j:Integer;
- j:=0;
- For i:=1 To 10 j:=i+j;
- ShowMessage(IntToStr(j));
复制代码 汇编代码- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Sample code for < Win32ASM Programming >
- ; by
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Model.asm
- ; 汇编语主模板
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 使用 nmake 或下列命令进行编译和链接:
- ; ml /c /coff Model.asm
- ; rc Model.rc
- ; Link /subsystem:windows Model.obj Model.res
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- .386
- .model flat, stdcall
- option casemap :none
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Include 文件定义
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- include windows.inc
- include user32.inc
- includelib user32.lib
- include kernel32.inc
- includelib kernel32.lib
- include Gdi32.inc
- includelib Gdi32.lib
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Equ 等值定义
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ICO_MODEL equ 102
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 数据段
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- .data?
- hInstance dd ?
- hWinMain dd ?
- .const
- fmt db "和是:%d", 0
- szClassName db 'Model',0
- szMessage db '用汇编语言写得的一个程序!', 0
- sLen dd $ - szMessage - 1
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 代码段
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- .code
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- _ProcWinMain proc uses ebx edi esi hWnd,uMsg,wParam,lParam
- local @stPS:PAINTSTRUCT
- local @strs[255] :byte
- local @hdc :HDC
- local @len:DWORD
-
- mov eax,uMsg
- .if eax == WM_PAINT
- invoke BeginPaint,hWnd,addr @stPS
-
- ;增加绘画代码
- mov eax, 0
- mov ecx, 1
- LP: add eax, ecx
- inc ecx
- cmp ecx, 100
- jle LP
-
- invoke wsprintf, addr @strs, addr fmt, eax
-
- lea edi, @strs
- mov esi, edi
- _StrCmp: cmp byte ptr[edi], 0
- jz _StrEnd
- inc edi
- jmp _StrCmp
- _StrEnd:
- sub edi, esi
- mov @len, edi
-
- invoke GetWindowDC, hWnd
- mov @hdc, eax
- invoke TextOut, @hdc, 20, 40, addr @strs, @len
- invoke ReleaseDC, hWnd, @hdc
-
- invoke EndPaint,hWnd,addr @stPS
- ;********************************************************************
- .elseif eax == WM_CLOSE
- invoke DestroyWindow,hWinMain
- invoke PostQuitMessage,NULL
- ;********************************************************************
- .else
- invoke DefWindowProc,hWnd,uMsg,wParam,lParam
- ret
- .endif
- ;********************************************************************
- xor eax,eax
- ret
- _ProcWinMain endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- _WinMain proc
- local @stWndClass:WNDCLASSEX
- local @stMsg:MSG
- invoke GetModuleHandle,NULL
- mov hInstance,eax
- ;********************************************************************
- ; 注册窗口类
- ;********************************************************************
- invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
- invoke LoadIcon,hInstance,ICO_MODEL
- mov @stWndClass.hIcon,eax
- mov @stWndClass.hIconSm,eax
- invoke LoadCursor,0,IDC_ARROW
- mov @stWndClass.hCursor,eax
- push hInstance
- pop @stWndClass.hInstance
- mov @stWndClass.cbSize,sizeof WNDCLASSEX
- mov @stWndClass.style,CS_HREDRAW or CS_VREDRAW
- mov @stWndClass.lpfnWndProc,offset _ProcWinMain
- mov @stWndClass.hbrBackground,COLOR_WINDOW + 1
- mov @stWndClass.lpszClassName,offset szClassName
- invoke RegisterClassEx,addr @stWndClass
- ;********************************************************************
- ; 建立并显示窗口
- ;********************************************************************
- invoke CreateWindowEx,WS_EX_CLIENTEDGE,\
- offset szClassName,offset szClassName,\
- WS_OVERLAPPEDWINDOW,\
- 100,100,450,370,\
- NULL,NULL,hInstance,NULL
- mov hWinMain,eax
- invoke ShowWindow,hWinMain,SW_SHOWNORMAL
- invoke UpdateWindow,hWinMain
- ;********************************************************************
- ; 消息循环
- ;********************************************************************
- .while TRUE
- invoke GetMessage,addr @stMsg,NULL,0,0
- .break .if eax == 0
- invoke TranslateMessage,addr @stMsg
- invoke DispatchMessage,addr @stMsg
- .endw
- ret
- _WinMain endp
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- start:
- call _WinMain
- invoke ExitProcess,NULL
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- end start
复制代码 下面是VB代码- Private Sub Form_Click()
- Dim a As Integer, i As Integer
- a = 0
- For i = 1 To 10
- a = a + i
- Next i
- Form1.Print a
- End Sub
复制代码 简单一点的汇编程序- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Sample code for < Win32ASM Programming >
- ; by **
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Sum2.asm
- ; 求1+2+3……100的和
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 使用 nmake 或下列命令进行编译和链接:
- ; ml /c /coff Sum2.asm
- ; rc Sum2.rc
- ; Link /subsystem:windows Sum2.obj Sum2.res
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- .386
- .model flat, stdcall
- option casemap :none
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; Include 文件定义
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- include windows.inc
- include user32.inc
- includelib user32.lib
- include kernel32.inc
- includelib kernel32.lib
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 数据段
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- .data
- szBuffer byte 1024 dup (?)
- .const
- fmt db "和是:%d", 0
- szCaption db "求1+2+3……100的和", 0
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- ; 代码段
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- .code
- start:
-
- mov eax, 0
- mov ecx, 1
- LP: add eax, ecx
- inc ecx
- cmp ecx, 100
- jle LP
-
- invoke wsprintf, addr szBuffer, addr fmt, eax
- invoke MessageBox,NULL,offset szBuffer,offset szCaption,MB_OK
- invoke ExitProcess,NULL
- ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- end start
复制代码
[ 本帖最后由 freesoft 于 2007-8-11 07:51 编辑 ] |
|