Ct215: Assembly Language Programming

Slides:



Advertisements
งานนำเสนอที่คล้ายกัน
E-COMMERCE WEBSITE Smartzap Co., Ltd.. Company Profile บริษัท สมาร์ทแซป จำกัด ก่อตั้งเมื่อปี 2543 (13 ปี ) ในช่วงยุค Internet เพิ่ง เริ่มต้น เป็นบริษัทที่ดำเนินงานทางด้าน.
Advertisements

John Rawls  John Rawls is the most famous American social contract theorist argued that “Justice is fairness” He Thought human natural have a appropriate.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Java Programming Language.
คำสั่ง DISPLAY รูปแบบที่ 1 DISPLAY identifier-1, identifier-2 … literal-1 literal-2 [ UPON mnemonic-name ]  ตัวอย่าง DISPLAY STUDENT-NAME. DISPLAY.
1 C Programming An Introduction. 2 Preprocessing Directives เขียนได้ 2 รูปแบบ #include คอมไพเลอร์จะทำ การค้นหาเฮดเดอร์ไฟล์ที่ระบุ จากไดเร็คทอรีที่ใช้
Set is a basic term in Mathematics. There is no precise definition for term “set”, But roughly speaking, a set is a collection of objects, Things or symbols,
Arithmetic Verb Template. ADD MOVE ZERO TO NO. ADD 1 TO NO. 0 1.
ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม (ต่อ)
จำนวน สถานะ NUMBER OF STATES. ประเด็นที่ สนใจ The number of distinct states the finite state machine needs in order to recognize a language is related.
Chapter 10 : Finalizing Design Specification
Chapter 9 : Designing Approach
Graphical User Interface charturong.ee.engr.tu.ac.th/CN208
Braille OCR Mobile Application
รู้จักกับเทคโนโลยี RFID เบื้องต้น
VARIABLES, EXPRESSION and STATEMENTS. Values and Data Types Value เป็นสิ่งพื้นฐาน มีลักษณะเป็น ตัวอักษร หรือ ตัวเลข อาทิ 2+2 หรือ “Hello world” Value.
อาจารย์ มธ. อธิบายการใช้ โมเดลของ
Data Structures and Algorithms
โครงสร้างข้อมูลแบบรายการโยง (Linked Lists) Data Structures and Algorithms อ. ธารารัตน์ พวงสุวรรณ คณะวิทยาศาสตร์และศิลปศาสตร์ มหาวิทยาลัยบูรพา วิทยาเขตสารสนเทศจันทบุรี
ระบบการจัดเก็บในคลังสินค้า
CS Assembly Language Programming
CS Assembly Language Programming Period 35.
: Chapter 1: Introduction 1 Montri Karnjanadecha ac.th/~montri Image Processing.
Color Standards A pixel color is represented as a point in 3-D space. Axis may be labeled as independent colors such as R, G, B or may use other independent.
ออโตมาตาจำกัด FINITE AUTOMATA
FINITE STATE AUTOMATA WITH OUTPUT
REGULAR EXPRESSION การบรรยายแบบสม่ำเสมอ
Helping you make better treatment decisions for your patients.
Shell Script Programming (Function)
Chapter 19 Network Layer: Logical Addressing
Inductive, Deductive Reasoning ผศ.( พิเศษ ) น. พ. นภดล สุชาติ พ. บ. M.P.H.
Course Software Engineering SE Overview and Introduction.
Problem with Subjunctive Verbs Some verbs and noun require a subjunctive. A subjunctive is a change in the usual form of the verb. It is often a verb word.
คำสั่ง READ รูปแบบ READ file-name RECORD [INTO identifier] [AT END imperative- statement] ตัวอย่าง READ STUDENT-FILE AT END GO TO END-PROCESS. READ CUSTOMER-FILE.
INC341 Steady State Error Lecture 6.
Stored Procedure.
การออกแบบและพัฒนาซอฟต์แวร์ บทที่ 7 การทดสอบโปรแกรม
Functions Standard Library Functions User-defined Functions.
CS Assembly Language Programming Period 7.
ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม
8/3/2014The Realities of software Testing1 Software testing Realities What is the realities of software testing Why does the software testing not complete.
Merchant Marine Training Centre วิชาการเป็นเลิศ เชิดชู คุณธรรม ผู้นำ.
COS2014: Assembly Language Programming
ภาควิชาวิทยาศาสตร์ประยุกต์ คณะวิทยาศาสตร์เทคโนโลยีและการเกษตร
Page: 1 โครงสร้างคอมพิวเตอร์ และภาษาแอสเซมบลี้ ผศ. บุรินทร์ รุจจน พันธุ์.. ปรับปรุง 19 ตุลาคม 2555 Introduction to Batch.
Menu and Interactive with Powerpoint ให้นำเรื่อง Input /Output Technology มา จัดทำ การนำเสนอ โดยใช้หลักการ Menu and Interactive with powerpoint มาประยุกต์
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of computer program.
ตัวแปรในภาษา GML ง การสร้างเกม คอมพิวเตอร์.
วิธีการ Auto ship.
Introduction to Earned Value Analysis.
Writing a research. Why Research?  To find whether the messages and the materials are appropriate to the target group  To modify the messages and the.
Java Programming Language สาขาวิชาระบบสารสนเทศ คณะบริหารธุรกิจ มหาวิทยาลัยเทคโนโลยีราชมงคลกรุงเทพ.
STACK ADT By Pantharee S.. Stack Model  A list with the restriction that insertions deletions can be performed in only one position (LIFO)  Push – insert.
List ADTs By Pantharee S..
ผัก. หน่อไม้ ฝรั่ง กะหล่ำ ปลี แค รอท กะหล่ำ ดอก.
การสร้าง WebPage ด้วย Java Script Wachirawut Thamviset.
Chapter 3 Simple Supervised learning
Chapter 1/1 Arrays. Introduction Data structures are classified as either linear or nonlinear Linear structures: elements form a sequence or a linear.
21 August ดรุณี ศมาวรรตกุล 1 2. ADT List - Unsorted list ADT - list implementation - Sorted List - Circular list - Doubly linked list.
Microprocessor and Interfacing PIC Flash Memory Segments 2K
Vi ธวัชชัย เอี่ยมไพโรจน์. เรื่องทั่วๆไปของ vi ถ้าเป็น server ที่ใช้ร่วมกันหลายๆคน อย่าง น้อยจะต้องมี vi และ emacs ไว้ให้ใช้ vi commands มีมากมาย แต่ที่ใช้บ่อยมัไม่
 Mr.Nitirat Tanthavech.  HTML forms are used to pass data to a server.  A form can contain input elements like text fields, checkboxes, radio-buttons,
Programming & Algorithm
Timed Math Quiz. โปรแกรมสุ่มคำนวณเลขแข่งกับ เวลา.
INC 161 , CPE 100 Computer Programming
การออกแบบระบบ System Design.
โปรแกรมภาษาแอสเซมบลี้เบื้องต้น
Forces and Laws of Motion
Lesson 7-6: Function Operations
สารสนเทศศาสตร์เบื้องต้น
Year 9 Term 1 Foundation (Unit 1) INTEGERS, ROUNDING AND PLACE VALUE
ใบสำเนางานนำเสนอ:

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 (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

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

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

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.

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

ตัวอย่างการแสดงผลในการพิมพ์ค่า "1234" ที่จอภาพ : ตัวอย่างการแสดงผลในการพิมพ์ค่า "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

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)

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

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

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

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

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

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

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

Example 1 การเคลียจอภาพ (Clear the screen) และให้โปรแกรมหน่วงเวลา 500 milliseconds และแสดงผลลัพธ์ในรีจีสเตอร์ลัค่าของแฟลก . .code call Clrscr mov eax,500 call Delay call DumpRegs 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 Sample output:

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

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

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

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.

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

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.

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

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

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=FFFFFFFF ESI=00000000 EDI=00130178 EBP=0012FFF0 ESP=0012FFC4 EIP=0040100A EFL=00000246 CF=0 SF=0 ZF=1 OF=0

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

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

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

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

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 .data ranNum 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

Input Parameters: None 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  

hexval dword ? .code call readhex mov hexval,eax 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

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

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

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

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

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 write Output: None Example:     mov eax,216543     call WriteInt Output as on console: +216543

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

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 WriteHex ; display hexadecimal Sample output: 0000 0000 0000 0000 0000 0000 0010 0011 35 23

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

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

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

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

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.

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).

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

PUSH and POP Instructions PUSH syntax: PUSH r/m16 PUSH r/m32 PUSH imm32 POP syntax: POP r/m16 POP r/m32

Using PUSH and POP push esi ; push registers push ecx push ebx 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

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

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.

Related Instructions PUSHFD and POPFD push and pop the EFLAGS register PUSHAD pushes the 32-bit general-purpose registers on the stack order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI POPAD pops the same registers off the stack in reverse order PUSHA and POPA do the same for 16-bit registers

Defining and Using Procedures Creating Procedures Documenting Procedures Example: SumOf Procedure CALL and RET Instructions Nested Procedure Calls Local and Global Labels Procedure Parameters Flowchart Symbols USES Operator

Creating Procedures Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java or C++ function Following is an assembly language procedure named sample: sample PROC . ret sample ENDP

Documenting Procedures Suggested documentation for each procedure: A description of all tasks accomplished by the procedure. Receives: A list of input parameters; state their usage and requirements. Returns: A description of values returned by the procedure. Requires: Optional list of requirements called preconditions that must be satisfied before the procedure is called. If a procedure is called without its preconditions having been satisfied, the procedure's creator makes no promise that it will work.

Example: SumOf Procedure ;----------------------------------------------------- SumOf PROC ; ; Calculates and returns the sum of three 32-bit integers. ; Receives: EAX, EBX, ECX, the three integers. May be ; signed or unsigned. ; Returns: EAX = sum, and the status flags (Carry, ; Overflow, etc.) are changed. ; Requires: nothing ;--------------------------------------------------------- add eax,ebx add eax,ecx ret SumOf ENDP

CALL and RET Instructions The CALL instruction calls a procedure pushes offset of next instruction on the stack copies the address of the called procedure into EIP The RET instruction returns from a procedure pops top of stack into EIP

CALL-RET Example (1 of 2) main PROC 00000020 call MySub 00000025 mov eax,ebx . main ENDP MySub PROC 00000040 mov eax,edx ret MySub ENDP 0000025 is the offset of the instruction immediately following the CALL instruction 00000040 is the offset of the first instruction inside MySub

(stack shown before RET executes) CALL-RET Example (2 of 2) The CALL instruction pushes 00000025 onto the stack, and loads 00000040 into EIP The RET instruction pops 00000025 from the stack into EIP (stack shown before RET executes)

Nested Procedure Calls By the time Sub3 is called, the stack contains all three return addresses:

Local and Global Labels A local label is visible only to statements inside the same procedure. A global label is visible everywhere. main PROC jmp L2 ; error L1:: ; global label exit main ENDP sub2 PROC L2: ; local label jmp L1 ; ok ret sub2 ENDP

Procedure Parameters (1 of 3) A good procedure might be usable in many different programs but not if it refers to specific variable names Parameters help to make procedures flexible because parameter values can change at runtime

Procedure Parameters (2 of 3) The ArraySum procedure calculates the sum of an array. It makes two references to specific variable names: ArraySum PROC mov esi,0 ; array index mov eax,0 ; set the sum to zero mov ecx,LENGTHOF myarray ; set number of elements L1: add eax,myArray[esi] ; add each integer to sum add esi,4 ; point to next integer loop L1 ; repeat for array size mov theSum,eax ; store the sum ret ArraySum ENDP What if you wanted to calculate the sum of two or three arrays within the same program?

Procedure Parameters (3 of 3) This version of ArraySum returns the sum of any doubleword array whose address is in ESI. The sum is returned in EAX: ArraySum PROC ; Receives: ESI points to an array of doublewords, ; ECX = number of array elements. ; Returns: EAX = sum ;----------------------------------------------------- mov eax,0 ; set the sum to zero L1: add eax,[esi] ; add each integer to sum add esi,4 ; point to next integer loop L1 ; repeat for array size ret ArraySum ENDP

Flowchart Symbols The following symbols are the basic building blocks of flowcharts: (Includes two symbols not listed on page 166 of the book.)

Flowchart for the ArraySum Procedure

USES Operator Lists the registers that will be preserved ArraySum PROC USES esi ecx mov eax,0 ; set the sum to zero etc. MASM generates the code shown in gold: ArraySum PROC push esi push ecx . pop ecx pop esi ret ArraySum ENDP

When not to push a register The sum of the three registers is stored in EAX on line (3), but the POP instruction replaces it with the starting value of EAX on line (4): SumOf PROC ; sum of three integers push eax ; 1 add eax,ebx ; 2 add eax,ecx ; 3 pop eax ; 4 ret SumOf ENDP

Program Design Using Procedures Top-Down Design (functional decomposition) involves the following: design your program before starting to code break large tasks into smaller ones use a hierarchical structure based on procedure calls test individual procedures separately

Integer Summation Program (1 of 4) Description: Write a program that prompts the user for multiple 32-bit integers, stores them in an array, calculates the sum of the array, and displays the sum on the screen. Main steps: Prompt user for multiple integers Calculate the sum of the array Display the sum

Procedure Design (2 of 4) Main Clrscr ; clear screen PromptForIntegers WriteString ; display string ReadInt ; input integer ArraySum ; sum the integers DisplaySum WriteInt ; display integer

gray indicates library procedure Structure Chart (3 of 4) gray indicates library procedure

Sample Output (4 of 4) Enter a signed integer: 550 The sum of the integers is: +431