Introduction to assembly learning notes (IX) - call and ret

Publisher:CuriousMind123Latest update time:2016-06-24 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Crazy summer study: Introduction to assembly learning notes (Part 9) - call and ret

 

Reference: "Assembly Language" by Wang Shuang, Chapter 10

 

Both call and ret are transfer instructions.

 

1. ret and retf

 

ret instruction: Use the data in the stack to modify the IP content, thereby achieving a near transfer

is equivalent to:

pop ip

retf instruction: Use the data in the stack to modify CS and IP to achieve far transfer

is equivalent to:

 

pop ip

 

 

pop cs

 

 

Example: ret

 

  1. assume cs:code,ss:stack  
  2.   
  3. stack segment  
  4.     db 16 dup(1)  
  5. stack ends  
  6.   
  7. code segment  
  8.     mov ax,4c00H  
  9.     int 21H  
  10.   
  11. start: mov ax,stack  
  12.     mov ss,ax  
  13.     mov sp,16  
  14.       
  15.     mov ax,0  
  16.     push ax  
  17.     ret  
  18.       
  19. code ends  
  20.   
  21. end start  

 

retf

 

  1. assume cs:code,ss:stack  
  2.   
  3. stack segment  
  4.     db 16 dup(1)  
  5. stack ends  
  6.   
  7. code segment  
  8.     mov ax,4c00H  
  9.     int 21H  
  10.   
  11. start: mov ax,stack  
  12.     mov ss,ax  
  13.     mov sp,16  
  14.       
  15.     mov ax,0  
  16.     push cs  
  17.     push ax  
  18.           
  19.     retf  
  20.       
  21. code ends  
  22.   
  23. end start  


 

2. Call instruction

 

Call instruction, execute operation:

    1. Push the current IP or CS and IP into the stack

    2. Jump

 

(1) Call instruction that transfers based on displacement

 

 

Format: call label

 

Push the next instruction's IP into the stack and go to the label

 

is equivalent to:

 

push ip

jmp near ptr label

 

 

 

(2) The transfer destination address is in the call instruction

 

Format:

call far ptr label

 

Push the CS and IP of the next instruction into the stack and jump to the label

 

is equivalent to:

push cs

push ip

jmp far ptr

 

(3) Call instruction with the transfer address in a register

 

Format: call 16-bit reg

 

is equivalent to:

push ip

jmp 16-bit reg

 

(4) Call instruction with transfer address in memory

   1. call word ptr memory unit

        is equivalent to:

             push ip

             jmp word ptr memory unit

   2. call dword ptr memory unit

        is equivalent to:

              push cs

              push ip

              jmp dword ptr memory location

 

3. mul instruction

 

mul is the multiplication instruction

 

Indicates the multiplication of two numbers, which must both be 8 bits or both 16 bits

 

The 8-bit multiplication result is stored in ax by default.

The high bit of the 16-bit multiplication result is stored in dx, and the low bit is stored in ax

See examples below.

 

 

3. Use call and ret together

 

Call combined with ret is equivalent to a function.

 

Example: Calculate the cube of the value in dw. Treat bx as the "function" parameter and ax as the return value of the "function".

 

  1. assume cs:code,ds:data  
  2.   
  3. data segment  
  4.     dw 1,2,3,4,5,6,7,8  
  5.     dd 0,0,0,0,0,0,0,0  
  6. data ends  
  7.   
  8. code segment  
  9.     start: mov ax,data  
  10.         mov ds,ax  
  11.           
  12.         mov si,0  
  13.         mov di,16  
  14.         mov cx,8  
  15.     s: mov bx,ds:[si]  
  16.         call cube  
  17.         mov ds:[di],ax  
  18.         mov ds:[di+2],dx  
  19.           
  20.         add si,2  
  21.         add di,4  
  22.         loops  
  23.               
  24.         mov ax,4c00H  
  25.         int 21H  
  26.               
  27.               
  28.     cube: mov ax,bx  
  29.         mul bx  
  30.         mul bx  
  31.         ret  
  32.               
  33.                   
  34. code ends  
  35.   
  36. end start  

 

The number of registers is limited. If there are too many parameters to be passed or returned, you can use memory or the stack.

 

Example: Convert lowercase to uppercase. (Use memory to store parameters)

 

  1. assume cs:code,ds:data  
  2.   
  3. data segment  
  4.     db 'conversation'  
  5. data ends  
  6.   
  7. code segment  
  8.     start: mov ax,data  
  9.         mov ds,ax  
  10.           
  11.         mov si,0  
  12.         mov cx,12  
  13.         call captial  
  14.           
  15.         mov ax,4c00H  
  16.         int 21H  
  17.               
  18. capital:and byte ptr ds:[si],11011111b  
  19.         inc si  
  20.         loop captial  
  21.               
  22. code ends  
  23.   
  24. end start  

 

Example: Calculate (a - b) ^3 Assume a=3, b=1 (Use stack to store parameters)

 

  1. assume cs:code  
  2.   
  3. code segment  
  4.     start: mov ax,1  
  5.         push ax  
  6.         mov ax,3  
  7.         push ax  
  8.         call difcube  
  9.           
  10.         mov ax,4c00H  
  11.         int 21H  
  12.           
  13. difcube:push bp  
  14.         mov bp,sp  
  15.         mov ax,[bp+4]  
  16.         sub ax,[bp+6]  
  17.         mov bp,ax  
  18.         mul bp  
  19.         mul bp  
  20.         pop bp  
  21.           
  22.         ret 4  
  23. code ends  
  24.   
  25. end start  

 

In the above code, ret 4 means:

pop ip

add sp,n

Example: Convert lowercase to uppercase, using 0 as the end. (Use stack to handle register conflicts)

 

  1. assume cs:code,ds:data  
  2.   
  3. data segment  
  4.     db 'word',0  
  5.     db 'city',0  
  6.     db 'good',0  
  7. data ends  
  8.   
  9. code segment  
  10.     start: mov ax,data  
  11.         mov ds,ax  
  12.         mov cx,3  
  13.           
  14.         mov bx,0  
  15.     s: push cx  
  16.         mov si,bx  
  17.         call capital  
  18.         add bx,5  
  19.         pop cx  
  20.         loops  
  21.               
  22.         mov ax,4c00H  
  23.         int 21H  
  24.               
  25. capital:mov cl,[si]  
  26.         mov ch,0  
  27.         jcxz  
  28.         and byte ptr [si],11011111b  
  29.         inc si  
  30.         jmp short capital  
  31.         ok: ret       
  32. code ends  
  33.   
  34. end start  

Note: Use the stack to save cx

 

Example: Implement the show_str "function" to display a string on the screen. Use dh to specify the function, dl to specify the column number, and cl to specify the color.

 

  1. assume cs:code,ds:data,ss:stack  
  2.   
  3. data segment  
  4.     db 'Welcome to masm!',0  
  5. data ends  
  6.   
  7. stack segment  
  8.     dw 8 dup(0)  
  9. stack ends  
  10.   
  11. code segment  
  12.     start: mov ax,data  
  13.         mov ds,ax  
  14.         mov ax,stack  
  15.         mov ss,ax  
  16.         mov sp,16  
  17.           
  18.         mov dh,10 ; line  
  19.         mov dl,17 ; column  
  20.         mov cl,2 ;color  
  21.         mov si,0  
  22.         call show_str  
  23.               
  24.         mov ax,4c00h  
  25.         int 21h  
  26.           
  27. show_str: push ax  
  28.         push di  
  29.         push dx  
  30.               
  31.         mov ax,10 ; determine the line segment es  
  32.         mul dh  
  33.         add ax,0b800h  
  34.         mov es,ax  
  35.           
  36.         mov dh,0 ; determine the column offset di, note that one character is two bytes  
  37.         add dx,dx  
  38.         mov di,dx  
  39.               
  40.               
  41.     s: push cx ; save cx  
  42.               
  43.         mov ch,0  
  44.         mov cl,ds:[si]  
  45.         jcxz ok; if it is 0, jump  
  46.           
  47.         mov es:[di],cl  
  48.         pop cx  
  49.         mov es:[di+1],cl  
  50.           
  51.         inc si  
  52.         add di,2  
  53.         jmp short s  
  54.               
  55.               
  56.               
  57.     ok: pop cx ; don't forget to pop, otherwise the IP restored by rec will be wrong  
  58.         pop dx  
  59.         pop di  
  60.         pop ax  
  61.         ret  
  62.               
  63.   
  64. code ends  
  65.   
  66. end start  

Reference address:Introduction to assembly learning notes (IX) - call and ret

Previous article:Notes on learning assembly language (10) - Flag register, string transfer instructions
Next article:Notes on learning assembly language (VIII) - Transfer instructions

Recommended ReadingLatest update time:2024-11-16 19:51

Samsung launches Anycall special edition earphone case to pay tribute to classic flip phone
      Anycall is a mobile phone brand founded by Samsung Electronics in South Korea in 1993 and later merged into the Samsung brand. Samsung today launched a special edition of Anycall earphone protective case, which will be sold in limited quantities before January 31.   The headphone case is available in two differe
[Mobile phone portable]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号