Empty string: Difference between revisions

Content deleted Content added
Bubbler (talk | contribs)
No edit summary
Line 28:
 
The check for a non-empty string is the same, but with "not" after the n:=
 
=={{header|AArch64 Assembly}}==
 
Declare an empty string at address <code>str</code>:
 
<lang ARM_Assembly>str: .asciz ""</lang>
 
Check if a string stored at <code>x0</code> is empty:
 
<lang ARM_Assembly> mov x5, #0
ldrb w5, [x0]
cmp x5, #0</lang>
 
Full program demo:
 
{{works with|aarch64-linux-gnu-as/qemu-aarch64}}
 
<lang ARM_Assembly>.equ STDOUT, 1
.equ SVC_WRITE, 64
.equ SVC_EXIT, 93
 
.text
.global _start
 
_start:
stp x29, x30, [sp, -16]!
ldr x0, =str1
mov x29, sp
bl str_empty // str_empty("");
ldr x0, =str2
bl str_empty // str_empty("non-empty");
ldp x29, x30, [sp], 16
mov x0, #0
b _exit
 
str1: .asciz ""
str2: .asciz "non-empty"
.align 4
 
// void str_empty(const char *s) - print "String is empty" if s is empty, "String is not empty" otherwise
str_empty:
mov x5, #0
ldrb w5, [x0]
ldr x1, =msg_empty
ldr x3, =msg_not_empty
mov x2, #16
mov x4, #20
cmp x5, #0
csel x1, x1, x3, eq // msg = s[0] == 0 ? msg_empty : msg_not_empty;
csel x2, x2, x4, eq // len = s[0] == 0 ? 16 : 20;
mov x0, #STDOUT
b _write // write(stdout, msg, len);
 
msg_empty:
.ascii "String is empty\n"
msg_not_empty:
.ascii "String is not empty\n"
.align 4
 
//////////////// system call wrappers
// ssize_t _write(int fd, void *buf, size_t count)
_write:
stp x29, x30, [sp, -16]!
mov x8, #SVC_WRITE
mov x29, sp
svc #0
ldp x29, x30, [sp], 16
ret
 
// void _exit(int retval)
_exit:
mov x8, #SVC_EXIT
svc #0</lang>
 
=={{header|ACL2}}==