; -----------------------------------------------------------------------------
; A 64-bit command line application to compute x^y.
;
; Syntax: power x y
; x and y are (32-bit) integers
; -----------------------------------------------------------------------------
global main
extern printf
extern puts
extern atoi
section .text
main:
push r12 ; save callee-save registers
push r13
push r14
; By pushing 3 registers our stack is already aligned for calls
cmp rdi, 3 ; must have exactly two arguments
jne error1
mov r12, rsi ; argv
; We will use ecx to count down form the exponent to zero, esi to hold the
; value of the base, and eax to hold the running product.
mov rdi, [r12+16] ; argv[2]
call atoi ; y in eax
cmp eax, 0 ; disallow negative exponents
jl error2
mov r13d, eax ; y in r13d
mov rdi, [r12+8] ; argv
call atoi ; x in eax
mov r14d, eax ; x in r14d
mov eax, 1 ; start with answer = 1
check:
test r13d, r13d ; we're counting y downto 0
jz gotit ; done
imul eax, r14d ; multiply in another x
dec r13d
jmp check
gotit: ; print report on success
mov rdi, answer
movsxd rsi, eax
xor rax, rax
call printf
jmp done
error1: ; print error message
mov edi, badArgumentCount
call puts
jmp done
error2: ; print error message
mov edi, negativeExponent
call puts
done: ; restore saved registers
pop r14
pop r13
pop r12
ret
answer:
db "%d", 10, 0
badArgumentCount:
db "Requires exactly two arguments", 10, 0
negativeExponent:
db "The exponent may not be negative", 10, 0
;(function()
{
// CommonJS
SyntaxHighlighter = SyntaxHighlighter || (typeof require !== 'undefined'? require('shCore').SyntaxHighlighter : null);
function Brush()
{
// Contributed by William Jojo
// https://programmingby.design
var datatypes = 'RESB RESW RESD RESQ REST RESO RESDQ RESY RESZ '
+ 'DB DW DD DQ DT DO DY DZ DDQ '
+ 'byte word dword qword tword oword yword zword ptr'
;
var keywords = 'bits default section segment absolute extern global common static '
+ 'equ times prefix gprefix lprefix postfix gpostfix lpostfix cpu float'
;
this.regexList = [
{ regex: SyntaxHighlighter.regexLib.singleQuotedString, css: 'string' },
{ regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' },
{ regex: /%?[re]?(ax|bx|cx|dx|sp|bp)/gi, css: 'color1' }, // registers
{ regex: /%?([csdefg]s|[abcd][lh]|(si|di)[hl])/gi, css: 'color1' }, // registers
{ regex: /%?((frp|mmx)[0-7]|xmm1[0-5]|xmm[0-9])/gi, css: 'color1' }, // registers
{ regex: /\$?(-?\.?)(\b(\d*\.?\d+|\d+\.?\d*)(e[+-]?\d+)?|0x[a-f\d]+)\b\.?/gi, css: 'color2' }, // numbers
{ regex: new RegExp(this.getKeywords(datatypes), 'gmi'), css: 'variable' }, // datatypes
{ regex: new RegExp(this.getKeywords(keywords), 'gmi'), css: 'keyword' },
{ regex: /;.*$/, css: 'comments' },
{ regex: /^(\.|\.\.@)?[a-zA-Z0-9]+:?/, css: 'comments' }
];
this.forHtmlScript(SyntaxHighlighter.regexLib.aspScriptTags);
};
Brush.prototype = new SyntaxHighlighter.Highlighter();
Brush.aliases = ['asm'];
SyntaxHighlighter.brushes.Asm = Brush;
// CommonJS
typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
})();