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
- assume cs:code,ss:stack
- stack segment
- db 16 dup(1)
- stack ends
- code segment
- mov ax,4c00H
- int 21H
- start: mov ax,stack
- mov ss,ax
- mov sp,16
- mov ax,0
- push ax
- ret
- code ends
- end start
retf
- assume cs:code,ss:stack
- stack segment
- db 16 dup(1)
- stack ends
- code segment
- mov ax,4c00H
- int 21H
- start: mov ax,stack
- mov ss,ax
- mov sp,16
- mov ax,0
- push cs
- push ax
- retf
- code ends
- 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".
- assume cs:code,ds:data
- data segment
- dw 1,2,3,4,5,6,7,8
- dd 0,0,0,0,0,0,0,0
- data ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov si,0
- mov di,16
- mov cx,8
- s: mov bx,ds:[si]
- call cube
- mov ds:[di],ax
- mov ds:[di+2],dx
- add si,2
- add di,4
- loops
- mov ax,4c00H
- int 21H
- cube: mov ax,bx
- mul bx
- mul bx
- ret
- code ends
- 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)
- assume cs:code,ds:data
- data segment
- db 'conversation'
- data ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov si,0
- mov cx,12
- call captial
- mov ax,4c00H
- int 21H
- capital:and byte ptr ds:[si],11011111b
- inc si
- loop captial
- code ends
- end start
Example: Calculate (a - b) ^3 Assume a=3, b=1 (Use stack to store parameters)
- assume cs:code
- code segment
- start: mov ax,1
- push ax
- mov ax,3
- push ax
- call difcube
- mov ax,4c00H
- int 21H
- difcube:push bp
- mov bp,sp
- mov ax,[bp+4]
- sub ax,[bp+6]
- mov bp,ax
- mul bp
- mul bp
- pop bp
- ret 4
- code ends
- 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)
- assume cs:code,ds:data
- data segment
- db 'word',0
- db 'city',0
- db 'good',0
- data ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov cx,3
- mov bx,0
- s: push cx
- mov si,bx
- call capital
- add bx,5
- pop cx
- loops
- mov ax,4c00H
- int 21H
- capital:mov cl,[si]
- mov ch,0
- jcxz
- and byte ptr [si],11011111b
- inc si
- jmp short capital
- ok: ret
- code ends
- 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.
- assume cs:code,ds:data,ss:stack
- data segment
- db 'Welcome to masm!',0
- data ends
- stack segment
- dw 8 dup(0)
- stack ends
- code segment
- start: mov ax,data
- mov ds,ax
- mov ax,stack
- mov ss,ax
- mov sp,16
- mov dh,10 ; line
- mov dl,17 ; column
- mov cl,2 ;color
- mov si,0
- call show_str
- mov ax,4c00h
- int 21h
- show_str: push ax
- push di
- push dx
- mov ax,10 ; determine the line segment es
- mul dh
- add ax,0b800h
- mov es,ax
- mov dh,0 ; determine the column offset di, note that one character is two bytes
- add dx,dx
- mov di,dx
- s: push cx ; save cx
- mov ch,0
- mov cl,ds:[si]
- jcxz ok; if it is 0, jump
- mov es:[di],cl
- pop cx
- mov es:[di+1],cl
- inc si
- add di,2
- jmp short s
- ok: pop cx ; don't forget to pop, otherwise the IP restored by rec will be wrong
- pop dx
- pop di
- pop ax
- ret
- code ends
- end start
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
- Popular Resources
- Popular amplifiers
- Modern Compiler Principles C Language Description (Ampel)
- A review of learning-based camera and lidar simulation methods for autonomous driving systems
- Multimodal perception parameterized decision making for autonomous driving
- Evaluating Roadside Perception for Autonomous Vehicles: Insights from Field Testing
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 2022 Digi-Key Innovation Design Competition】Material Unboxing Post-ESP32-S2-Kaluga-1
- [Application development based on NUCLEO-F746ZG motor] 7. Program framework - two important interrupts
- TI C66x four memory protection issues
- 【Iprober 520 Current Probe】Evaluation Report (Part 3) Practical Circuit Application
- HDMI to DP with 5V power supply adapter board design circuit diagram
- [Flower carving hands-on] Interesting and fun music visualization series of small projects (25) - water tornado vortex lamp
- New review! Zhongke Yihai Micro-Shenzhen series FPGA development board EQ6HL45 is here!
- 【GD32E231 DIY】RTC Development
- Fluke Award-winning Live Broadcast | Basics of Data Loggers, Their Applications and Calibration
- Regarding power supply, let me share a particularly inspiring story...