1 / 72

Ct215: Assembly Language Programming

Ct215: Assembly Language Programming. Chapter 5: Procedures. Procedures. Procedure Definition i.e., the code of the procedure routine Associated with a label Procedure Call Ideally, p (a 1 ,…,a n ) Very similar to a jump: JMP Label p

dorian-chan
Télécharger la présentation

Ct215: Assembly Language Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ct215: Assembly Language Programming Chapter 5: Procedures

  2. Procedures • Procedure Definition • i.e., the code of the procedure routine • Associated with a label • Procedure Call • Ideally, p (a1,…,an) • Very similar to a jump: JMP Labelp • …except that means of passing the parameters to p must be decided • One idea: pass parameters in registers • Another idea: pass them on the “stack” • If you want to know how your favorite programming language handle procedures, this is where you find out

  3. Chapter Overview • Using predefined procedures • Linking to an External Library • The Book's Link Library • Stack Operations • Defining and Using your own Procedures • Program Design Using Procedures

  4. The Book's Link Library • Link Library Overview • Calling a Library Procedure • Linking to a Library • Library Procedures – Overview • Six Examples

  5. Link Library Overview • A file containing procedures that have been compiled into machine code • constructed from one or more OBJ files • To build a library under Windows, . . . • start with one or more ASM source files • assemble each into an OBJ file • create an empty library file (extension .LIB) • add the OBJ file(s) to the library file, using the Microsoft LIB utility Take a quick look at Irvine32.asm – it contains a large number of useful procedures.

  6. Calling a Library Procedure การเรียก procedure จาก Libraryโดยการใช้คำสั่ง CALL บาง procedures ต้องการกำหนดค่าอินพุตของ arguments ซึ่งการทำงานคำสั่งเทียม INCLUDE directive จะทำสำเนารูปแบบของ procedure เพื่อแสดงให้เห็น Procedures ตัวอย่างการแสดงผลในการพิมพ์ค่า "1234" ที่จอภาพ : INCLUDE Irvine32.inc .code mov eax,1234h ; input argument call WriteHex ; show hex number call Crlf ; end of line

  7. ตัวอย่างการแสดงผลในการพิมพ์ค่า "1234" ที่จอภาพ : • INCLUDE Irvine32.inc; เป็นการประกาศในการใช้ Library • .code ; เป็นคำสั่งเทียมที่บอกค่าของ Code segment • mov eax,1234h; input argument • call WriteHex; show hex number • call Crlf; end of line • การทำงานในโปรแกรมจะมีการเชื่อมเข้ากับ Irvine32.lib โดยใช้คำสั่งในการจาก linker ที่อยู่ภายใต้ batch file ขื่อ make32.bat. • Notice the two LIB files: Irvine32.lib, and kernel32.lib

  8. Linking to a Library • Your programs link to Irvine32.lib using the linker command inside a batch file named make32.bat. • Notice the two LIB files: Irvine32.lib, and kernel32.lib • the latter is part of the Microsoft Win32 Software Development Kit (SDK)

  9. Library Procedures Clrscr – โปรแกรมย่อยนี้เป็นการเคลียจอภาพทั้งจอภาพ บรรทัด 0 คอลัมน์ 0 จนถึง บรรทัด 24 คอลัมน์ 79 . Crlf – โปรแกรมย่อยนี้เป็นการทำงานถึงการสิ้นสุดบรรทัดและขึ้นบรรทัดใหม่ Delay - โปรแกรมย่อยนี้เป็นการหน่วงเวลาในการเอ็กซีคิวส์คำสั่งที่กำหนดเป็น n millisecond interval.

  10. DumpMem - โปรแกรมย่อยนี้เป็นการพิมพ์ข้อมูลที่อยู่ในหน่วยความจำเป็นกลุ่มและแสดงผลในรูปเลขฐานสิบหก DumpRegs – โปรแกรมย่อยนี้เป็นการแสดงผลลัพธ์ข้อมูลของรีจีสเตอร์ EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP, EFLAGS, and EIP registers เป็นเลขฐานสิบหกและแสดงค่าของแฟลกที่เกิดขึ้นดังนี้ Carry, Sign, Zero, and Overflow flags.

  11. GetCommandtail - โปรแกรมย่อยนี้เป็นการทำสำเนาคำสั่งในโปรแกรมในรูปของอะเรย์ GetMseconds - Returns the number of milliseconds that have elapsed since midnight. Gotoxy – โปรแกรมย่อยนี้เป็นกำหนดตำแหน่งของเคอร์เซอร์บนจอภาพ. Random32 – โปรแกรมย่อยนี้เป็นการจ่ายค่าของการสุ่มข้อมูลเลขจำนวนเต็มขนาด 32 บิตระหว่าง 0 to FFFFFFFFh.

  12. Randomize - Seeds the random number generator. RandomRange – โปรแกรมย่อยนี้เป็นการสุ่มตามที่กำหนด . ReadChar - โปรแกรมย่อยนี้เป็นการอ่านค่าตัวอักขระจากหน่วยรับข้อมูลคือคีย์บอร์ด. ReadHex – โปรแกรมย่อยนี้เป็นการอ่านจำนวนเต็มเลขฐานสิบหกขนาด 32 บิตจากคีย์บอร์ดและสิ้นสุดการรับด้วยคีย์ Enter key.

  13. ReadInt – โปรแกรมย่อยนี้เป็นการอ่านค่าเลขจำนวนเต็มฐานสิบแบบคิดเครื่องหมายจากคีย์บอร์ดและสิ้นสุกด้วยคีย์ Enter key. ReadString – โปรแกรมย่อยนี้เป็นการอ่านค่าสตริงจากคีย์บอร์ดและสิ้นสุดด้วยคีย์ Enter key. SetTextColor – โปรแกรมย่อยนี้เป็นการกำหนเค่าสีของ foreground และ background ในการกำหนดค่าสีของ text บนจอภาพ.

  14. WaitMsg – โปรแกรมย่อยนี้เป็นการแสดงข้อความที่ปรากฏจนกว่าจะกดคีย์ Enter key WriteBin – โปรแกรมย่อยนี้เป็นการพิมพ์ค่าเลขจำนวนเต็มขนาด 32 บิตไม่คิดเครื่องหมายบนจอภาพในรูปของ ASCII binary format. WriteChar – โปรแกรมย่อยนี้เป็นการพิพม์ตัวอักขระที่ละตัวบนจอภาพ . WriteDec – โปรแกรมย่อยนี้เป็นการพิมพ์ค่าเลขจำนวนต็มขนาด 32 บิตไม่คิดเครื่องหมายบนจอภาพในรูปของ decimal format.

  15. WriteHex - โปรแกรมย่อยนี้เป็นการพิมพ์ค่าเลขจำนวนต็มขนาด 32 บิตไม่คิดเครื่องหมายบนจอภาพในรูปของ hexadecimal format. WriteInt - โปรแกรมย่อยนี้เป็นการพิมพ์ค่าเลขจำนวนต็มขนาด 32 บิตคิดเครื่องหมายบนจอภาพในรูปของ decimal format. WriteString – โปรแกรมย่อยนี้เป็นการพิมพ์ค่าสตริงบนจอภาพ.

  16. Sample output: EAX=00000613 EBX=00000000 ECX=000000FF EDX=00000000 ESI=00000000 EDI=00000100 EBP=0000091E ESP=000000F6 EIP=00401026 EFL=00000286 CF=0 SF=1 ZF=0 OF=0 Example 1 การเคลียจอภาพ (Clear the screen) และให้โปรแกรมหน่วงเวลา 500 milliseconds และแสดงผลลัพธ์ในรีจีสเตอร์ลัค่าของแฟลก . .code call Clrscr mov eax,500 call Delay call DumpRegs

  17. Example 2 การแสดงผลลัพธ์ของค่าสตริง และเลื่อนเคอร์เซอร์ไปยังจุดเริ่มต้นของบรรทัดแรกของจอภาพถัดไป. .data str1 BYTE "Assembly language is easy!",0 .code mov edx,OFFSET str1 call WriteString call Crlf

  18. Example 2a การแสดงผลลัพธ์ของค่าสตริง และเลื่อนเคอร์เซอร์ไปยังจุดเริ่มต้นของบรรทัดใหม่ของจอภาพ (use embedded .data str1 BYTE "Assembly language is easy!",0Dh,0Ah,0 .code mov edx,OFFSET str1 call WriteString

  19. Sample output: 0000 0000 0000 0000 0000 0000 0010 0011 35 23 Example 3 การแสดงผลลัพธ์ค่าจำนวนเต็มเลขฐานสองไม่คิดครื่องหมายเลชฐานสิบและเลขฐานสิบหกโดยแยกแต่ละบรรทัด. IntVal = 35 .code mov eax,IntVal call WriteBin ; display binary call Crlf call WriteDec ; display decimal call Crlf call WriteHex ; display hexadecimal call Crlf

  20. Example 4 การพิมพ์ค่าสตริงที่ป้อนเข้ามาของผู้ใช้ รีจีสเตอร์ EDX จะเป็นตัวชี้ค่าของสตริง และรีจีสเตอร์ ECX กำหนดค่าของตัวอักขระสูงสุดที่ผู้ใช้จะป้อนเข้ามา. .data fileName BYTE 80 DUP(0) .code mov edx,OFFSET fileName mov ecx,SIZEOF fileName – 1 call ReadString A null byte is automatically appended to the string.

  21. Example 5 เป็นการแสดงผลแบบสุ่มเลขชำนวนเต็มที่คิดเครื่องหมายในช่วง 0 – 99. เลขจำนวนเต็มแต่ละค่าใช้การพิมพ์ด้วยโปรแกรมย่อยที่เรียกว่า WriteInt และข้อมูลที่พิมพ์เก็บใน EAX และแสดผลที่ละบรรทัด .. .code mov ecx,10 ; loop counter L1:mov eax,100 ; ceiling value call RandomRange ; generate random int call WriteInt ; display signed int call Crlf ; goto next display line loop L1 ; repeat loop

  22. Example 6 เป็นการแสดงค่าของแอตทริบิวส์ของตัวสตริงที่มีพื้นที่สีน้ำเงินและตัวอักขระสีเหลือง .data str1 BYTE "Color output is easy!",0 .code mov eax,yellow + (blue * 16) call SetTextColor mov edx,OFFSET str1 call WriteString call Crlf The background color is multiplied by 16 before being added to the foreground color.

  23. Procedure Descriptions CLRSCR เป็นการทำงานในการเคลียจอภาพ Call Clrscr Crlf เป็นการทำงานของการสิ้นสุดบรรทัดและขึ้นบรรทัดใหม่ (0Dh,0Ah) หรือเป็นการเลื่อนเคอร์เซอร์ไปยังบรรทัดใหม่ของจอภาพ.    call Crlf

  24. Delay เป็นการทำงานในการหน่วงเวลาของโปรแกรมที่กำหนดค่ามาพร้อมกับคำสั่ง เมื่อการเรียกโปรแกรมย่อยนี้Delay procedure pauses the program for a specified time interval. When calling function, set EAX to desired interval, in milliseconds Mov eax, 1000 ; 1 second Call delay

  25. DumpRegs เป็นการแสดงค่าของรีจีสเตอร์ในปัจจุบันบนจอภาพ ซึ่งการใช้งานหลักจะเป็นการใช้งานของโปรแกรมดีบัก debugging. โปรแกรมย่อย DumpRegs จะพิมพ์ผลลัพธ์ข้อมูลในรีจีสเตอร์ รวมถึงค่าในแฟลก แต่การทำงานจะไม่เปลี่ยนแปลงค่าในรีจีสเตอร์ Example: call DumpRegs Example Output (CF=Carry Flag, SF=Sign Flag, ZF=Zero Flag and OF=Overflow Flag): EAX=00000005 EBX=7FFDF000 ECX=00000101 EDX=FFFFFFFFESI=00000000 EDI=00130178 EBP=0012FFF0 ESP=0012FFC4EIP=0040100A EFL=00000246 CF=0 SF=0 ZF=1 OF=0

  26. Dumpmem โปรแกรมย่อยนี้จะพิมพ์ผลลัพธ์ในรูปเลขฐานสิบหกเป็นกลุ่มในช่วงระหว่างที่กำหนดโดยรีจีสเตอร์ ESI , และจำนวนค่าที่แสดงเก็บในรีจีสเตอร์ ECX และขนาดของการพิมพ์เก็บใน EBX ( 1 = byte , 2 = word , 4 doubleword) .data array dword 1,2,3,4,5,6,7,8,9,0aH,0dH main proc mov esi,offset array mov ecx,lengthof array mov ebx,type array call dumpmem

  27. Exit การทำงานส่วนนี้เป็นแมคโคร ซึงเป็นส่วนของการสิ้นสุดโปรแกรมและให้กับไปที่ operating system. Example: main proc …….    exit main endp end

  28. Gotoxy เป็นการกำหนดตำแหน่งบรรทัดและคอลัมน์ของเคอเซอร์บนจอภาพ ค่าของตำแหน่งจะส่งผ่านไปยังรีจีสเตอร์ DH และ DL สามารถกำหนดในช่วง 0 to X-1 และจาก 0 to Y-1, ขณะที่ X คือค่าของคอลัมน์ และ Y คือค่าของบรรทัดบนจอภาพ เราจะกำหนดค่าบนจอภาพได้ 25 บรรทัด และ 80 คอลัมน์ . Input Parameters: DH = row DL = column  Example: mov dl,79 ;column mov dh,24 ;row call Gotoxy

  29. ReadChar เป็นการอ่านค่าตัวอักขระที่ละตัวจากคีย์บอร์ดและตัวอักขระจะถูกนำมาเก็บไว้ในรีจีสเตอร์ AL แต่ตัวอักขระจะไม่ปรากฏที่จอภาพ Output:  AL = ACSII code Example: datacharIn BYTE ? .code call ReadChar mov  charIn,al cmp charIn, 'A' je doMoveLeft

  30. RandomRange Generates an unsigned pseudo-random 32-bit integer in the range of 0 through (n-1). Input Parameters: EAX = n, the range Output:  EAX = random (0 to n-1) Example: ;Get a random number from 1 to 100 .dataranNum DWORD ? .code mov eax,100 ;get random 0 to 99 call RandomRange ; inc eax ;make range 1 to 100 mov ranNum,eax ;save random number

  31. ReadInt Reads a 32-bit signed decimal integer from standard input, stopping when the Enter key is pressed. All valid digits occurring before a non-numeric character are converted to the integer value. Leading spaces are ignored, and an optional leading + or - sign is permitted.  ReadInt will display an error message, set the Overflow flag, and reset EAX to zero if the value entered cannot be represented as a 32-bit signed integer. Input Parameters: None Output: if OF=0, EAX = valid binary value, and SF=sign. If OF=1, EAX = 0 (invalid input) Example: .data intNum DWORD ? .code call ReadInt mov intNum, eax

  32. ReadHex Readhex procedure reads a 32 bit hexadecimal integer form standard input and reurns the value in EAX .data hexval dword ? .code call readhex mov hexval,eax

  33. Readstring Readstring procedure reads a string form standard input. Stooping when the user process the Enter key. The following statements call Readstring ,passing ECX and EDX .data buffer byte 50 dup(0) bytecount dword ? .code mov edx,offset buffer ; point to buffer mov ecx,(Sizeof buffer); specify max charactera call readstring ;input the string mov bytecount,eax ;number of characters

  34. Example Input a string from the user. EDX points to the string and ECX specifies the maximum number of characters the user is permitted to enter. .data fileName BYTE 80 DUP(0) .code mov edx,OFFSET fileName mov ecx,SIZEOF fileName – 1 call ReadString

  35. WriteChar Writes a single character to standard output. Input Parameters: AL = the character to write Example: mov  al,'A' call WriteChar

  36. WriteDec Writes an unsigned 32-bit decimal number to standard output in decimal format with no leading zeros. Input Parameters: EAX = unsigned number to write Output:  none Example: mov eax,123456 call WriteDec Output: 123456

  37. WriteInt Writes a signed 32-bit decimal number to standard output in decimal format with a leading sign and no leading zeros. Input Parameters: EAX = signed number to writeOutput: NoneExample:    mov eax,216543    call WriteIntOutput as on console: +216543

  38. Writehex The write procedure writes a32 bit unsigned interger to standard output in 8 digit hexadecimal format. Mov eax,7FFFH Call writehex ;display :00007FFF Writebin Wirtebin procedure writes an to stabdard output in ASCII binary format Move ax,12345678H Call writebin ;display 0001 0010 0011 0100 0101 0110 0111 1000

  39. Example Display an unsigned integer in binary, decimal, and hexadecimal, each on a separate line IntVal = 35 .code mov eax,IntVal call WriteBin ; display binary call Crlf call WriteDec ; display decimal call Crlf call WriteHex ; display hexadecimal call Crlf Sample output: 0000 0000 0000 0000 0000 0000 0010 0011 35 23

  40. Example Generate and display ten pseudorandom signed integers in the range 0 – 99. Pass each integer to WriteInt in EAX and display it on a separate line .code mov ecx,10 ; loop counter L1: mov eax,100 ; ceiling value call RandomRange ; generate random int call WriteInt ; display signed int call Crlf ; goto next display line loop L1 ; repeat loop

  41. Stack Operations • Runtime Stack • PUSH Operation • POP Operation • PUSH and POP Instructions • Using PUSH and POP • Example: Reversing a String • Related Instructions

  42. Runtime Stack • Imagine a stack of plates . . . • plates are only added to the top • plates are only removed from the top • LIFO structure

  43. Runtime Stack • Managed by the CPU, using two registers • SS (stack segment) • ESP (stack pointer) * * SP in Real-address mode

  44. PUSH Operation (1 of 2) • A 32-bit push operation decrements the stack pointer by 4 and copies a value into the location pointed to by the stack pointer.

  45. PUSH Operation (2 of 2) • Same stack after pushing two more integers: The stack grows downward. The area below ESP is always available (unless the stack has overflowed).

  46. POP Operation • Copies value at stack[ESP] into a register or variable. • Adds n to ESP, where n is either 2 or 4. • value of n depends on the attribute of the operand receiving the data

  47. PUSH and POP Instructions • PUSH syntax: • PUSH r/m16 • PUSH r/m32 • PUSH imm32 • POP syntax: • POP r/m16 • POP r/m32

  48. Using PUSH and POP Save and restore registers when they contain important values. PUSH and POP instructions occur in the opposite order. push esi ; push registers push ecx push ebx mov esi,OFFSET dwordVal ; display some memory mov ecx,LENGTHOF dwordVal mov ebx,TYPE dwordVal call DumpMem pop ebx ; restore registers pop ecx pop esi

  49. Example: Nested Loop Remember the nested loop we created on page 129? It's easy to push the outer loop counter before entering the inner loop: mov ecx,100 ; set outer loop count L1: ; begin the outer loop push ecx ; save outer loop count mov ecx,20 ; set inner loop count L2: ; begin the inner loop ; ; loop L2 ; repeat the inner loop pop ecx ; restore outer loop count loop L1 ; repeat the outer loop

  50. Example: Reversing a String • Use a loop with indexed addressing • Push each character on the stack • Start at the beginning of the string, pop the stack in reverse order, insert each character back into the string • Source code • Q: Why must each character be put in EAX before it is pushed? Because only word (16-bit) or doubleword (32-bit) values can be pushed on the stack.

More Related