Guide to Assembly Language by James T. Streib
Author:James T. Streib
Language: eng
Format: epub
Publisher: Springer London, London
7.2 Complete Program: Implementing the Power Function in a Procedure
To illustrate a complete example, consider the problem of calculating x n from Chapter 5. Instead of having the code to calculate x n in the main program, it could be placed in a procedure. The procedure can then be invoked more than one time from the main program without having to duplicate the code each time. For the sake of simplicity both here in the C program and more importantly in the subsequent assembly language program, power is implemented as a procedure (void function) and x, n, and ans are implemented as global variables. In addition to outputting a message in the case of an error, the procedure also returns a -1 in the variable ans: #include <stdio.h> int x,n,ans; int main() { void power(); printf("%s","Enter x: "); scanf("%d",&x); printf("%s","Enter n: "); scanf("%d",&n); power(); printf("\n%s%d\n\n","The answer is: ",ans); return 0; } void power() { int i; ans=-1; if(x<0 || n<0) printf("\n%s\n","Error: Negative x and/or y"); else if(x==0 && n==0) printf("\n%s\n","Error: Undefined answer"); else { i=1; ans=1; while(i<=n) { ans=ans*x; i++; } } }
As mentioned previously, global variables are used for x, n, and ans both in the C program above and in the assembly language below. Since i is declared as a local variable in the C code above and is not needed in the main program, ecx is used as the loop control variable in the assembly language procedure below: .listall .386 .model flat,c .stack 100h scanf PROTO arg2:Ptr Byte, inputlist:VARARG printf PROTO arg1:Ptr Byte, printlist:VARARG .data in1fmt byte "%d",0 msg1fmt byte "%s",0 msg3fmt byte "%s%d",0Ah,0Ah,0 errfmt byte "%s",0Ah,0 errmsg1 byte 0Ah,"Error: Negative x and/or y",0 errmsg2 byte 0Ah,"Error: Undefined answer",0 msg1 byte "Enter x: ",0 msg2 byte "Enter n: ",0 msg3 byte 0Ah,"The answer is: ",0 x sdword ? n sdword ? ans sdword ? .code main proc INVOKE printf, ADDR msg1fmt, ADDR msg1 INVOKE scanf, ADDR in1fmt, ADDR x INVOKE printf, ADDR msg1fmt, ADDR msg2 INVOKE scanf, ADDR in1fmt, ADDR n call power INVOKE printf, ADDR msg3fmt, ADDR msg3, ans ret main endp power proc push eax ; save registers push ecx push edx mov ans,-1 ; default value for ans .if x<0 || n<0 INVOKE printf, ADDR errfmt, ADDR errmsg1 .else .if x==0 && n==0 INVOKE printf, ADDR errfmt, ADDR errmsg2 .else mov ecx,1 ; initialize ecx loop counter mov ans,1 ; initialize ans .while ecx <= n mov eax,ans ; load eax with ans imul x ; multiply eax by x mov ans,eax ; sotre eax in ans inc ecx ; increment eax loop countere .endw .endif .endif pop edx ; restore registers pop ecx pop eax ret power endp end
Could the assembly language procedure above use registers instead of global variables to communicate back and forth between the procedure and the main program? Yes, but in the procedure above, x and y are checked to see if they are negative in the .if directive. Recall from Chapter 4 that the default in high-level directives is unsigned data unless a memory location declared as sdword is used.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Computer Design | Control Systems |
DSPs | Embedded Systems |
Microprocessor Design | PIC Microcontroller |
Linux Device Driver Development Cookbook by Rodolfo Giometti(3907)
Embedded Programming with Modern C++ Cookbook by Igor Viarheichyk(3427)
Implementing Cellular IoT Solutions for Digital Transformation by Dennis McCain(3379)
Embedded Linux Development Using Yocto Project - Third Edition by Otavio Salvador & Daiane Angolini(3215)
TinyML Cookbook by Gian Marco Iodice(3160)
Simplifying 3D Printing with OpenSCAD by Colin Dow(2829)
TinyML Cookbook by Gian Marco Iodice & Ronan Naughton(2589)
Fusion 360 for Makers by Lydia Sloan Cline(2208)
Networking A Beginner's Guide by Bruce Hallberg(2196)
Hands-On Linux for Architects by Denis Salamanca(2032)
But How Do It Know? by J. Clark Scott(2017)
Computers For Seniors For Dummies by Nancy C. Muir(1986)
Raspberry Pi and MQTT Essentials by Dhairya Parikh(1938)
Arduino Project Handbook, Volume 2: 25 Simple Electronics Projects for Beginners by Geddes Mark(1935)
9781803246888-ENHANCING DEEP LEARNING WITH BAYESIAN INFERENCE by Unknown(1887)
Hack and HHVM by Owen Yamauchi(1872)
31 Days Before Your CompTIA A+ Exams (Shanette Luellen's Library) by Benjamin Patrick Conry(1848)
MicroPython Projects by Jacob Beningo(1713)
Hands-On Internet of Things with MQTT by Tim Pulver(1689)
